Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 334
Prashant Jain Avatar asked Nov 22 '17 07:11

Prashant Jain


People also ask

How do I manage multiple notifications on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

What is FCM notification in Android?

Firebase Cloud Messaging is a real-time solution for sending notifications to client apps without any kind of charges. FCM can reliably transfer notifications of up to 4Kb of payload.

What does FCM mean on my phone?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.


2 Answers

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.

like image 181
Prashant Jain Avatar answered Nov 24 '22 17:11

Prashant Jain


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.

like image 33
Scott B Avatar answered Nov 24 '22 16:11

Scott B