duannuan0074
2019-02-05 09:48 阅读 138

Android(至少在三星上)现在将所有应用程序分组,而无需更改应用程序代码或有效负载

I have since more than one year a cordova (7.1.0) App for both iOS and Android which relies for push notification on phonegap-plugin-push plugin.

All worked fine and is also working fine now except only the last notification is sent.

As my app can be set to monitor multiple places, it would make sense yes to show only the last notification BUT at least one for each selected place.

I thought about using "tag" but I tried putting it in different places without success.

Here the bare minimum PHP code I use to send the notifications:

$msg['tag']=$spotid; //tried on 2019 02 05: no success

$msg['sound']=$sound;
$msg['soundname']=$sound; //20180913
$msg['android_channel_id']=$sound; //20180913       

$data = array
(
        'Spot' => $spotname,
        'rain' => $rain
);
$fields = array
(
        'registration_ids' => $newId
        'vibrate'   => $vibration,
        'priority'  => 'high',  
        'data' => array_merge( $msg,$data ), //$data //20180913
        'tag' => $spotid   //tried on 2019 02 05: no success
);


$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$resultFCM = curl_exec($ch );
if ($resultFCM === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}
curl_close( $ch );

Any suggestion?

EDIT

After futher tests I found something that works:

$msg['tag']=$spotid; //works if inserted in "notification"

$msg['sound']=$sound;
$msg['soundname']=$sound; //20180913
$msg['android_channel_id']=$sound; //20180913       

$data = array
(
        'Spot' => $spotname,
        'rain' => $rain
);
    $fields = array
        (
            'registration_ids' => $newId, //$registrationIds,
            'vibrate'   => $vibration,
            'priority'  => 'high',  
             'notification' => $msg, // 2019 attempt to group ONLY by spot. check if problems with iOS
            'data' => array_merge( $msg,$data )
        );

To be noted that $msg must be fully repeated (and not just "tag" added) otherwise the Android notification will lack icon, sound and so on

It works and groups the notifications

The issue is, that clicking the notification it does not open anymore the app: I'm checking some suggestions here:

Android - Firebase Notification not opening targeted activity when app is in background but working properly in foreground

but not sure how to apply that in a cordova app...

EDIT 2

It ended up being much simpler than I thought.

First of all, phonegap-push-plugin doesn't "like" the "Notification" setting and one of the side effects about using it, is that it does not associate the click on notification action to opening the background (or closed) application which instead happens putting "data" in payload (without "notification").

As "tag" (which helps grouping) is supported in "notification" only (it seems) that's a no go for my case.

Nonetheless, in the documentation of the push plugin there is a wonderful hint on how to group which an element of the "data" array:

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#stacking

https://github.com/phonegap/phonegap-plugin-push/issues/2523

Following my example above where the grouping element is the integer $spotid I had just to add 'notId' => $spotid:

$msg['sound']=$sound;
$msg['soundname']=$sound; //20180913
$msg['android_channel_id']=$sound; //20180913         

$data = array
(
    'Spot' => $spotname,
    'rain' => $rain,
    'notId' => $spotid  // 2019 attempt to group ONLY by spot. check if      problems with iOS
);
$fields = array
    (
        'registration_ids' => $newId, //$registrationIds,
        'vibrate'   => $vibration,
        'priority'  => 'high'
        'data' => array_merge( $msg,$data )
    );

notId into "data" allows to properly group.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dqvtm82066 dqvtm82066 2019-02-05 16:50

    As per comments in Edit2, for cordova-plugin-push, adding "notId" into "data" allows to properly group (and/or separate in my case) notifications. See above edit for details

    点赞 评论 复制链接分享

相关推荐