我正在尝试在我的项目中集成FCM通知.我有云功能后端,应用程序在
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应用程序.