Firebase 中的 FCM 令牌是什么? [英] What is FCM token in Firebase?

查看:26
本文介绍了Firebase 中的 FCM 令牌是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

到底是什么?

一个 FCM 令牌,或者通常称为 registrationToken,例如 的问题.如 GCM FCM 文档:

<块引用>

由 GCM 连接服务器颁发给客户端应用程序的 ID,允许它接收消息.请注意,注册令牌必须保密.

<小时>

我怎样才能得到那个令牌?

更新:仍然可以通过调用 getToken() 来检索令牌,但是,根据 FCM 的最新版本,FirebaseInstanceIdService.onTokenRefresh()代码> 已被替换为 FirebaseMessagingService.onNewToken() -- 根据我的经验 功能相同正如 onTokenRefresh() 所做的.

<小时>

旧答案:

根据 FCM 文档:><块引用>

在您的应用首次启动时,FCM SDK 会为客户端应用实例生成一个注册令牌.如果您想定位单个设备或创建设备组,则需要访问此令牌.

您可以通过扩展 FirebaseInstanceIdService 来访问令牌的值.确保您已将该服务添加到清单中,然后在 onTokenRefresh 的上下文中调用 getToken,并记录如下所示的值:

@Override公共无效 onTokenRefresh() {//获取更新的 InstanceID 令牌.String refreshedToken = FirebaseInstanceId.getInstance().getToken();Log.d(TAG, "刷新令牌:" + refreshedToken);//TODO:实现此方法以将任何注册发送到您的应用程序的服务器.sendRegistrationToServer(refreshedToken);}

每当生成新令牌时 onTokenRefreshcallback 都会触发,因此在其上下文中调用 getToken 可确保您访问当前可用的注册令牌.如果令牌尚未生成,FirebaseInstanceID.getToken() 返回 null.

获得令牌后,您可以将其发送到您的应用服务器并使用您喜欢的方法进行存储.有关 API 的完整详细信息,请参阅实例 ID API 参考.

In the new Firebase, under Notification, they have mentioned that developer can send notification to a particular device. For that, in console it asks for an FCM token. What is it exactly and how can I get that token?

解决方案

What is it exactly?

An FCM Token, or much commonly known as a registrationToken like in . As described in the 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.


How can I get that token?

Update: The token can still be retrieved by calling getToken(), however, as per FCM's latest version, the FirebaseInstanceIdService.onTokenRefresh() has been replaced with FirebaseMessagingService.onNewToken() -- which in my experience functions the same way as onTokenRefresh() did.


Old answer:

As per the 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.

这篇关于Firebase 中的 FCM 令牌是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆