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

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

问题描述

在新的Firebase中,在通知下,他们提到开发人员可以向特定设备发送通知。为此,在控制台中请求一个FCM令牌。什么是确切的,我怎么能得到这个令牌?



一个FCM令牌,或者通常被称为 registrationToken 就像。如 GCM文档中所述:


由GCM连接服务器向客户端应用程序发出的ID,允许其接收消息。请注意,注册代币必须保密。







如何获得该标记?



根据
$ b


客户端应用程序实例的注册令牌。如果您想要定位单个设备或创建设备组,您需要访问该令牌。



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

  @Override 
public void onTokenRefresh(){
//获取更新的InstanceID标记。
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,Refreshed token:+ refreshedToken);

// TODO:实现此方法将任何注册发送到您的应用程序的服务器。
sendRegistrationToServer(refreshedToken);



$ b $ p
$ b

每当生成一个新的标记时,onTokenRefreshcallback就会触发,所以在上下文中调用getToken确保您正在访问当前可用的注册令牌。如果令牌尚未生成,FirebaseInstanceID.getToken()将返回null。

获得令牌后,可以将其发送到应用服务器并存储它使用你喜欢的方法。


请参阅实例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 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?

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天全站免登陆