Reputation: 43
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?
Upvotes: 1
Views: 202
Reputation: 663
// 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.
Upvotes: 2
Reputation: 73721
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
Upvotes: 2