gpt4 book ai didi

android - Firebase 中的 FCM token 是什么?

转载 作者:IT王子 更新时间:2023-10-28 23:56:40 27 4
gpt4 key购买 nike

在新的 Firebase 中,在通知下,他们提到开发人员可以向特定设备发送通知。为此,在控制台中它要求提供 FCM token 。它到底是什么,我怎样才能得到那个 token ?

最佳答案

究竟是什么?

一个 FCM token ,或者通常称为 registrationToken,如 .如 GCM FCM docs 中所述:

An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.


我怎样才能得到那个 token ?

更新:仍然可以通过调用 getToken() 来检索 token ,但是,根据 FCM 的最新版本,FirebaseInstanceIdService.onTokenRefresh() 已替换为 FirebaseMessagingService.onNewToken() -- 根据我的经验 functions the same way as onTokenRefresh() did .


旧答案:

根据 FCM docs :

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.

You can access the token's value by extending FirebaseInstanceIdService. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh, and log the value as shown:

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}

The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. FirebaseInstanceID.getToken() returns null if the token has not yet been generated.

After you've obtained the token, you can send it to your app server and store it using your preferred method. See the Instance ID API reference for full detail on the API.

关于android - Firebase 中的 FCM token 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671380/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com