Firebase云消息传递重复通知

11

我们正在使用Firebase云消息传递。有时候,当Android或iOS应用程序处于睡眠模式时,手机会收到相同的(重复的)通知消息。为了设备识别,使用FIRInstanceID令牌。一个基于node.js的外部服务器用于向Firebase服务发送通知。在我们的服务器日志文件中没有重复出现的记录。


Firebase有很多问题,但不包括这个。请再次检查您的服务器。问题必须出在您这边。 - Hisham Muneer
12
Firebase存在很多问题,同时也有这个问题。我可以证实在多台Android 4.4和5.0设备上出现了这种情况。我只使用Firebase控制台发送推送通知。 - Catalin Morosan
你是否恰好有服务器发送的示例负载?即使有一些已被编辑的值,它仍可能提供答案的线索。 - PGMacDesign
4个回答

2

在文件firebase-messaging-sw.js

注释掉这行代码

// self.registration.showNotification(notificationTitle,notificationOptions)

这样就不会有重复的fcm了


但是您也不再得到图标。 - Kerry Jones
对我来说,它起作用了,同时还有图片。 - alex44_lel

1
如果您正在使用Firebase-sw.js,则以下是发生的情况。
“请注意,这不适用于管理员SDK。”
当您在有效载荷中发送“notification”键时,它会触发浏览器的默认行为,并显示通知。然后,在您的服务提供商中,您再次显示通知:
self.registration.showNotification(notificationTitle,notificationOptions)

...你没有错,因为firebase文档没有提及任何细节。

解决方案 您需要从负载中删除“notification”键,并像这样修改firebase-sw.js文件:

messaging.onBackgroundMessage(function(payload) {
  console.log('Received background message ', payload);

  const notificationTitle = payload.data.title;
  const notificationOptions = {
    body: payload.data.body
  };
  self.registration.showNotification(notificationTitle, notificationOptions);
});

你的有效载荷应该长这样:

{
    "data": {
        "title":"New Ride Request",
        "body":"A new ride has been requested",
        "sound" : "default"
    },
    "condition": "'topic1' in topics"
}  

现在您的默认通知将不会触发,只会显示代码级别的通知。


0

-1

今天我用我的应用程序遇到了同样的问题,经过几个小时的调试,似乎问题出在我发送的负载上:

{
  "registration_ids":["token", "token2",...],
  "data":{
     "title":"notification title",
     "body":"notification body",
     "screen":"product",
     "id":1
    },
    "android":{
      "ttl":"1000s",
      "priority":"high",
      "notification":null //<= set notification to null 
    
  }
} 

将通知设置为 null 后,不会出现重复通知。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接