【问题标题】:Android : How to integrate Push Notification to an already existing app?Android:如何将推送通知集成到现有的应用程序中?
【发布时间】:2019-11-20 06:04:50
【问题描述】:

如何将推送通知集成到现有的安卓应用中?

我的 Android 应用程序已经在 Playstore 上可用,现在我想在下一个版本中集成推送通知。我已经使用 FCM 和 AWS SNS 实现了它。

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

【问题讨论】:

  • FirebaseMessagingService will get call when we installs the app first time only. 你为什么这么认为?
  • onNewToken 在我们第一次安装应用程序时被调用。如果我们更新应用程序,它不会被调用。
  • onNewToken 不会在应用安装时调用,它会在令牌更改时调用。

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


【解决方案1】:
// 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 获取令牌并将其发送到后端。或者,我们也可以首先在我们的首选项中检查令牌,然后只向 firebase 请求令牌。

【讨论】:

    【解决方案2】:

    问题是:FirebaseMessagingService 的 onNewToken 方法会得到 仅在我们新安装应用程序时调用。但是当我们更新它时 onNewToken 方法永远不会被调用。所以我们不能在 更新应用程序时的 AWS 门户。请高手指点如何 在现有应用中实现此功能?

    你可以打电话

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

    随时获取实例 id 以推送到您的服务器,这是一个阻塞调用,因此请确保在后台线程上执行此操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2020-07-12
      相关资源
      最近更新 更多