FCM循环推送主题通知时新通知覆盖旧通知,如何实现多条通知单独展示
解决方法
通知被覆盖的核心原因是你没有给每条推送设置唯一的通知标识,安卓系统默认会用同一个标识的新通知覆盖旧通知,按照下面的步骤修改即可:
1. 服务端PHP代码调整
在推送参数中加入android配置项,给每条通知设置唯一的tag或者notification_id,可以直接用循环里的$item['tagid']或者循环索引作为唯一标识,修改后的代码如下:
foreach ($result as $key => $item) { $message = array ( 'body' => "Details:".$item['tagid'], 'title' => "Tag:".$item['event'], 'key1' => "NotificationActivity" ); $fields = array( 'to' => '/topics/all', 'data' => array ( 'message' => $message ), // 新增安卓专属配置 'android' => array( 'notification' => array( // 这里用唯一值,确保每条通知标识不同 'tag' => 'notify_'.$item['tagid'], // 也可以用循环的索引值$key作为唯一ID 'notification_id' => intval($item['tagid']) ) ) ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $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)); $result = curl_exec($ch); curl_close($ch); }
2. 数据消息自渲染场景的安卓端额外调整
如果你的APP是接收FCM的data类型消息后,自行调用系统通知API创建通知,需要确保NotificationManager.notify()方法的第二个参数ID是唯一值,不能所有通知都传同一个固定ID:
// 错误示例:所有通知都用同一个固定ID 0,会导致覆盖 notificationManager.notify(0, notification); // 正确示例:传入和当前通知对应的唯一ID即可 notificationManager.notify(uniqueId, notification);
内容的提问来源于stack exchange,提问作者Bilal
相关产品推荐
相关产品推荐

