9

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.

  • Maybe your event onUpdate fires multiple times since your status changed ? – Arrabidas92 Nov 22 '17 at 10:16
  • @Arrabidas92 no its not getting fired more than once. for this testing I am updating that 'status' property manually just once (so i know it is getting fired only once). And also the logs show console.log statements only once. And another evidence is that in notification message I show a random number, so the message will be different if the code is executed again. Looks like some kind of notification retry strategy. 2,4,8,16,32 mins etc – Jo Dar Gaya Woh Mar Gaya Nov 22 '17 at 10:50
  • not just this, but data notification are also showing up multiple notifications – Jo Dar Gaya Woh Mar Gaya Nov 27 '17 at 00:02

2 Answers2

2

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.

1

I just had this same issue occur with my Ionic Android app. The same notification repeated after 2, 4, and 8 minutes. This seems to be an issue on the client side because it even happens when sending a message directly from the Firebase console.

I tried several things to fix it and it seems like the only way I could get it to work as intended was to make a new Android project and new Firebase app.

Scott B
  • 51
  • 1
  • 3