duannuan0074 2019-02-05 09:48
浏览 140
已采纳

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 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面