一年多以来,我有一个适用于 iOS 和 Android 的 cordova (7.1.0) 应用程序,它依赖于 phonegap-plugin-push 插件上的推送通知。
除了最后一个通知被发送外,一切正常,现在也正常工作。
由于我的应用可以设置为监控多个地方,因此只显示最后一个通知但每个选定地点至少显示一个通知是有意义的。
我考虑过使用“标签”,但我尝试将它放在不同的地方但没有成功。
这里是我用来发送通知的最基本的 PHP 代码:
$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 );
有什么建议吗?
编辑
经过进一步的测试,我发现了一些有用的东西:
$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 )
);
需要注意的是 $msg 必须完全重复(而不仅仅是添加“标签”)否则Android通知将缺少图标,声音等
它工作并将通知分组
问题是,点击通知它不再打开应用程序:我正在检查这里的一些建议:
但不确定如何在 cordova 应用程序中应用它...
编辑 2
结果比我想象的要简单得多。
首先,phonegap-push-plugin 不“喜欢”“通知”设置,使用它的副作用之一是它不会将点击通知操作与打开后台(或关闭)应用程序,它恰好将“数据”放入有效负载(没有“通知”)。
因为“标签”(有助于分组)仅在“通知”中受支持(似乎),这对我来说是行不通的。
尽管如此,在推送插件的文档中有一个关于如何对“数据”数组的元素进行分组的绝妙提示:
https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#stacking
https://github.com/phonegap/phonegap-plugin-push/issues/2523
按照我上面的示例,其中分组元素是整数 $spotid 我只需要添加 '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 到“数据”允许正确分组。
最佳答案
根据 Edit2 中的评论,对于 cordova-plugin-push,将“notId”添加到“data”中可以正确分组(和/或在我的情况下分开)通知。 有关详细信息,请参见上面的编辑
关于php - Android(至少在三星上)现在正在对所有应用程序进行分组,而无需更改应用程序代码或有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531583/