Android : How to integrate Push Notification to an already existing app?

42
July 11, 2019, at 7:20 PM

How to integrate Push Notification to an already existing android app?

My android app is already available on playstore and now I wants to integrate Push Notification in next release. I have implemented it using FCM and AWS SNS.

Problem is : onNewToken method of FirebaseMessagingService will get called only when we installed the app freshly. But when we update it onNewToken method never gets call. So we cannot register the token on AWS portal while updating the app. Experts please advise how to implement this in existing app?

Answer 1

Problem is : onNewToken method of FirebaseMessagingService will get called only when we installed the app freshly. But when we update it onNewToken method never gets call. So we cannot register the token on AWS portal while updating the app. Experts please advise how to implement this in existing app?

You can call

FirebaseInstanceId.getInstance().getToken(senderId,"FCM");

At anytime to get an instance id to push to your server, this is a blocking call so make sure to do it on a background thread

Answer 2
// Use this in your splashscreen or dashboard view.    
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
        if (!task.isSuccessful)
            return@addOnCompleteListener
        if(prefs.pushNotificationToken == "") {
            //log the token
            prefs.pushNotificationToken = task.result?.token?.trim() ?: ""
            //send user push notification token to the server(use Patch instead of Post)
        }
}

Doing this, both old and new user will have "pushNotificationToken" in prefs to be empty. Thus, we can fetch the token any time from firebase and send it to the backend.Or, also first we can check for token in our prefs and then only ask to firebase for token.

READ ALSO
Unexpected behaviour of onChildAdded stream in firebase_database

Unexpected behaviour of onChildAdded stream in firebase_database

I've recently started learning Flutter and the FlutterFire pluginsYesterday I was working with the firebase_database plugin which allows adding Firebase Realtime Database to Flutter

61
How to use SQLite in android fragments?

How to use SQLite in android fragments?

I am an aspiring android developerI wanted to make a simple project with SQLtite support, but I ran into the problem of SQLite whith Fragment

45
can we make custom notification show inside the android studio on the top of the screen instead the status bar?

can we make custom notification show inside the android studio on the top of the screen instead the status bar?

I need to create a list of custom notifications each time just one notification will appear on the screen but I need those notifications to show inside the APP instead of the status bar I am using android 9 so we need to use CHNNEL_IDIn case we can do that, will it be considered...

64
IllegalStateException when adding Fragment from Backstack

IllegalStateException when adding Fragment from Backstack

From time to time I get the following crash report:

62