FCM:onNewToken 与 FirebaseInstanceId

FCM: onNewToken vs FirebaseInstanceId(FCM:onNewToken 与 FirebaseInstanceId)
本文介绍了FCM:onNewToken 与 FirebaseInstanceId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase 已弃用 com.google.firebase:firebase-messaging:17.1.0 版本中的一些消息调用.post 很好地介绍了这些更改.

Firebase has deprecated some of the messaging calls in the com.google.firebase:firebase-messaging:17.1.0 release. This post goes over those changes nicely.

问题:有人能告诉我不实现 onNewToken 是否被认为是不好的做法,而是在每次应用启动时调用下面的块 这对于 Android 人来说可能似乎过分了但从 iOS 的角度来看,感觉就像家一样.

Question: Can someone tell me if is it considered bad practice to not implement onNewToken and instead just call the below block each app launch This probably seems excessive to Android folks but feels like home from an iOS point of view.

FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(this) { instanceIdResult ->
    // Just use this call 
    val newToken = instanceIdResult.token
    Log.i("newToken", newToken)
}



@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    // Leave this unimplemented
}

我更熟悉 iOS,它在 每个 应用程序启动时调用其 onNewToken 等效项.所以对于 iOS,我把逻辑放在那里以确定我的后端是否需要更新.

I am more familiar with iOS which calls its onNewToken equivalent on every app launch. So for iOS I put logic there to determine if my backend needs to be updated.

getInstanceId() 文档说 如果它尚不存在,则会生成一个实例 ID,它会开始定期向 Firebase 后端发送信息.这让我假设我可以在每次启动时调用 FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener.

getInstanceId() docs say This generates an Instance ID if it does not exist yet, which starts periodically sending information to the Firebase backend. This makes me assume I can just call FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener each launch.

推荐答案

有些事情非常很重要,但还没有人提到:

Something very important that no one has mentioned yet:

如果您仅在应用启动后检查当前设备令牌,您可能会在应用处于后台时丢失令牌更新事件(当然),并且您将无法接收远程推送消息从您的服务器直到用户再次启动应用程序并将新令牌发送到服务器.

If you check for the current device token only after an app launch, you might loose the event of the token being updated while your app is in the background (of course) and you won't be able to receive remote push messages from your server until the user launches the app again and you send the new token to the server.

在您的应用程序处于后台时也可以调用该回调的全部目的是防止丢失后端消息(如果您的应用程序或其某些重要功能在很大程度上依赖于推送通知,这一点很重要).请务必注意,此回调不仅会在您首次注册设备时为您提供令牌,而且 also: 如果 InstanceID 令牌更新则调用.如果先前令牌的安全性受到威胁,则可能会发生这种情况.

The whole purpose of having that callback which can also be called while your app is in the background is to prevent loosing backend messages (important if your app or some important features of it relies a lot on push notifications). It is important to be aware that this callback will not only deliver you the token when you register the device for the first time but also: Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised.

所以:

谁能告诉我不实现 onNewToken 而是在每个应用程序启动时调用下面的块是否被认为是不好的做法这对 Android 人来说可能看起来有点过分,但从 iOS 的角度来看就像家一样.

Can someone tell me if is it considered bad practice to not implement onNewToken and instead just call the below block each app launch This probably seems excessive to Android folks but feels like home from an iOS point of view.

是的,不实现 onNewToken() 实际上是一种不好的做法.

Yes, it is actually a bad practice to not implement onNewToken().

这篇关于FCM:onNewToken 与 FirebaseInstanceId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)