14

如何从 Firebase 获取设备令牌(FCM 注册令牌)?

我正在使用 firebase 制作移动应用程序,并使用 node.js 制作其服务器。

我想push notification从服务器发送消息,但我不知道如何获取设备令牌。

  1. 如何从外部服务器获取设备令牌?我现在正在使用 Firebase 管理 SDK。

  2. 设备令牌是否仅在应用连接 fcm 服务器时生成?

  3. 当用户首次运行应用程序并注册 FCM 服务器时,我应该将令牌保存到另一个数据库吗?

4

4 回答 4

12

1. How to get device tokens from external server? I'm using Firebase admin SDK now.

There is currently no API to retrieve all the Registration tokens for your app from the Server side. It's the developer's (you) responsibility to send and store the Registration token to your App Server, which is generated from the Client App side.

2. Is the device token only generated when the app is connected FCM server?

The documentation pretty much covered this (see also my answer here):

On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

It doesn't technically get connected to the FCM Servers. It connects to the FirebaseInstanceID service (via the SDK), which in turn generates the Registration Token.

3. Should I save token to another database when user first run the app and register FCM server?

As I mentioned in #1, you should save it where you can easily access it to send a message.

于 2017-03-28T05:15:07.503 回答
3

The device token is generated when the app first connects, this token must then be stored in your database, and replaced if a new token is assigned

public class MyAndroidFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "MyAndroidFCMIIDService";

    @Override
    public void onTokenRefresh() {
        //Get hold of the registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        //Log the token
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
        //Implement this method if you want to store the token on your server
    }
}

See the following Tutorial

于 2017-03-28T05:10:24.960 回答
2

我建议根本不存储令牌。存储令牌似乎是个好主意,直​​到您意识到您的用户有多个设备(iOS 设备、Android 设备、桌面等)。现在您必须管理每个用户存储多个设备令牌,这可能会很快变得混乱。

相反,忽略生成的令牌并简单地为每个用户订阅一个主题,例如当用户登录或创建帐户时,为他们订阅由他们的用户 ID 组成的主题

NSString* useridchannel = [NSString stringWithFormat:@"user_%ld", (long)userid];
[[FIRMessaging messaging] subscribeToTopic:useridchannel completion:^(NSError * _Nullable error) {
       NSLog(@"Subscribed user to topic %@", useridchannel);
}];

当用户注销时,请确保取消他们对该主题的订阅,因为如果他们不再登录您的应用程序,他们可能不想收到通知

NSString* useridchannel = [NSString stringWithFormat:@"user_%ld", (long)userid];
[[FIRMessaging messaging] unsubscribeFromTopic:useridchannel completion:^(NSError * _Nullable error) {
         NSLog(@"Unsubscribed user from %@", useridchanel);
}];

我意识到设备组应该用于此目的,但我不想再为存储和管理令牌而烦恼,而且 Firebase Admin SDK for PHP 不支持向设备组发送消息。通过使用主题,无需处理设备令牌,您可以轻松地从服务器、应用程序或 Firebase 控制台向所有用户设备发送消息。

于 2020-02-20T14:43:58.983 回答
0

您可以向

https://<<your_project_name>>.firebaseio.com/.json?auth=<<your_database_secret_key>>

您在选项卡下的 Firebase 控制台中获得的数据库密钥settings/serviceaccounts/databasesecrets

这将返回一个 JSON 文件,其中包含您项目的所有存储数据。

于 2019-07-31T17:17:47.480 回答