Search code examples
androidfirebasefirebase-cloud-messagingfirebase-notifications

why do I receive multiple FCM notifications on android 7


I am trying to integrate FCM notification in my project. I have Cloud Function backend, and app runs on android. Below is cloud code to send notification:

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)
    })
})

On native Android app, I receive notification multiple times with interval of few mins. Here, I have seen notification like this:

Notification Test 30

then after 2,4,8,16,32 mins of previous notification time I again get below message

Notification Test 30

I don't think I need to paste log here, because code is definitely executed just once (as the random number in notification stays the same).

So, why is this happening and how to fix it?

Below is my environment:

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

Please try to reproduce in environment mentioned above.


Solution

  • I have resolved the issue by renaming my application package name eg old name: com.xyz to com.xyz2

    Using the new name i added this (new) android app to firebase project (it generated new app id). And the notifications started worked as expected (no retry).

    But its a shame that I have to rename app package to resolve it. If this app was released in google play then I could not have renamed the app, else no one can get further updates on this app, and it becomes a new app!

    It would still be great if some firebase developers could shed light on what is happening.

    Recreating firebase project and recreating android project did not help when app name / top level package name were the same. Changing app name and relevant namespaces in existing android project fixed it for now.

    Ideally I would like to know the proper fix for this and use the existing name rather than suffixing 2 at the end of app name.