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

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测