Android:如何将Push Notification集成到现有应用程序中?

时间:2019-07-10 15:04:54

标签: android push-notification firebase-cloud-messaging

如何将Push Notification集成到现有的android应用中?

我的android应用已经在playstore上可用,现在我想在下一个版本中集成Push Notification。我已经使用FCM和AWS SNS实现了它。

问题是:仅当我们重新安装应用程序时,FirebaseMessagingService的onNewToken方法才会被调用。但是,当我们更新它时,onNewToken方法永远不会被调用。因此,在更新应用程序时,我们无法在AWS门户上注册令牌。专家请建议如何在现有应用中实现此目标?

2 个答案:

答案 0 :(得分: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)
        }
}

这样做,新老用户都将在首选项中将“ pushNotificationToken”设置为空。因此,我们可以随时从firebase提取令牌并将其发送到后端。或者,首先,我们可以在prefs中检查令牌,然后仅向firebase请求令牌。

答案 1 :(得分:1)

  

问题是:FirebaseMessagingService的onNewToken方法将获得   仅在我们全新安装应用程序时调用。但是当我们更新它   onNewToken方法永远不会被调用。因此我们无法在以下位置注册令牌   更新应用程序时使用AWS门户。专家请指教   在现有的应用程序中实现?

您可以打电话

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

任何时候获取实例ID推送到您的服务器,这都是阻塞调用,因此请确保在后台线程上进行

相关问题