为什么我在 android 7 上收到多个 FCM 通知
why do I receive multiple FCM notifications on android 7
我正在尝试将 FCM 通知集成到我的项目中。我有 Cloud Function 后端,应用程序在 android 上运行。
以下是发送通知的云代码:
exports.notificationTest = functions.database.ref(`/test/childA/childB/status`).onUpdate(event => {
const status = event.data.val();
console.info("Processing notificationTest cloud function")
console.info(`status : ${status}`)
const token = "EnterYourNotificationTokenHere"
const randomNum = Math.floor(Math.random() * 100)
var message = {
notification: { title : "My App", body : `Notification Test ${randomNum}`}
}
console.info(`Sending message on notification token`)
return admin.messaging().sendToDevice(token, message)
.then((response) => {
console.info("Successfully sent notification")
}).catch(function(error) {
console.warn("Error sending notification " , error)
})
})
在本机 Android 应用程序上,我每隔几分钟收到多次通知。
在这里,我看到了这样的通知:
Notification Test 30
然后在之前通知时间的 2、4、8、16、32 分钟后,我再次收到以下消息
Notification Test 30
我认为我不需要在这里粘贴日志,因为代码肯定只执行一次(因为通知中的随机数保持不变)。
那么,为什么会发生这种情况以及如何解决它?
下面是我的环境:
Native Android App
Using Android 7
Latest Android Studio Stable
Android Gradle Plugin - 3.1.1
Gradle - 4.1
Firebase-Ui - 3.1.0
Play Services - 11.4.2
请尝试在上述环境中重现
我的 Ionic Android 应用程序也出现了同样的问题。 2、4 和 8 分钟后重复相同的通知。这似乎是客户端的一个问题,因为它甚至在直接从 Firebase 控制台发送消息时也会发生。
我尝试了几种方法来修复它,看来我能让它按预期工作的唯一方法是制作一个新的 Android 项目和新的 Firebase 应用程序。
我已经通过重命名我的应用程序包名称解决了这个问题
例如旧名称:com.xyz 到 com.xyz2
我使用新名称将此(新)android 应用程序添加到 firebase 项目(它生成了新的应用程序 ID)。并且通知开始按预期工作(没有重试)。
但很遗憾,我必须重命名应用程序包才能解决它。如果这个应用程序是在 google play 中发布的,那么我就不能重命名这个应用程序,否则没有人能得到这个应用程序的进一步更新,它就变成了一个新的应用程序!
如果一些 Firebase 开发人员能够阐明正在发生的事情,那就太好了。
当应用程序名称/顶级包名称相同时,重新创建 firebase 项目和重新创建 android 项目没有帮助。更改现有 android 项目中的应用程序名称和相关命名空间暂时修复了它。
理想情况下,我想知道对此的正确修复并使用现有名称而不是在应用程序名称末尾添加后缀 2。
我正在尝试将 FCM 通知集成到我的项目中。我有 Cloud Function 后端,应用程序在 android 上运行。 以下是发送通知的云代码:
exports.notificationTest = functions.database.ref(`/test/childA/childB/status`).onUpdate(event => {
const status = event.data.val();
console.info("Processing notificationTest cloud function")
console.info(`status : ${status}`)
const token = "EnterYourNotificationTokenHere"
const randomNum = Math.floor(Math.random() * 100)
var message = {
notification: { title : "My App", body : `Notification Test ${randomNum}`}
}
console.info(`Sending message on notification token`)
return admin.messaging().sendToDevice(token, message)
.then((response) => {
console.info("Successfully sent notification")
}).catch(function(error) {
console.warn("Error sending notification " , error)
})
})
在本机 Android 应用程序上,我每隔几分钟收到多次通知。 在这里,我看到了这样的通知:
Notification Test 30
然后在之前通知时间的 2、4、8、16、32 分钟后,我再次收到以下消息
Notification Test 30
我认为我不需要在这里粘贴日志,因为代码肯定只执行一次(因为通知中的随机数保持不变)。
那么,为什么会发生这种情况以及如何解决它?
下面是我的环境:
Native Android App
Using Android 7
Latest Android Studio Stable
Android Gradle Plugin - 3.1.1
Gradle - 4.1
Firebase-Ui - 3.1.0
Play Services - 11.4.2
请尝试在上述环境中重现
我的 Ionic Android 应用程序也出现了同样的问题。 2、4 和 8 分钟后重复相同的通知。这似乎是客户端的一个问题,因为它甚至在直接从 Firebase 控制台发送消息时也会发生。
我尝试了几种方法来修复它,看来我能让它按预期工作的唯一方法是制作一个新的 Android 项目和新的 Firebase 应用程序。
我已经通过重命名我的应用程序包名称解决了这个问题 例如旧名称:com.xyz 到 com.xyz2
我使用新名称将此(新)android 应用程序添加到 firebase 项目(它生成了新的应用程序 ID)。并且通知开始按预期工作(没有重试)。
但很遗憾,我必须重命名应用程序包才能解决它。如果这个应用程序是在 google play 中发布的,那么我就不能重命名这个应用程序,否则没有人能得到这个应用程序的进一步更新,它就变成了一个新的应用程序!
如果一些 Firebase 开发人员能够阐明正在发生的事情,那就太好了。
当应用程序名称/顶级包名称相同时,重新创建 firebase 项目和重新创建 android 项目没有帮助。更改现有 android 项目中的应用程序名称和相关命名空间暂时修复了它。
理想情况下,我想知道对此的正确修复并使用现有名称而不是在应用程序名称末尾添加后缀 2。