我可以为 firebase 使用特定的令牌吗?

Can I use specific token for firebase?

我正在开发一个 React Native 应用程序并且我使用 Firebase 推送通知服务。 我有用户,所有用户都有自己的用户令牌,我想使用这些用户令牌发送推送通知。

首先我想,当用户登录到应用程序时,我可以将用户令牌注册到主题,但我也不确定。

那么有什么方法可以使用自定义令牌向特定用户发送推送通知吗?

您可以为特定用户发送通知:

'to' => *users token*,
'notification' => [
        'body' =>  $message,
        title' => $title,
                    
],

Firebase Cloud Messaging 没有用户的概念。它只知道所谓的 FCM 令牌,它标识特定设备上特定安装的应用程序。

如果您使用的是 react-native-firebase 库,则可以从 saving tokens. Once you have the token, you can us the token to send a message to that specific app on that specific device 上的文档中了解如何获取 FCM 令牌。

如果您想在用户使用(并登录)您的应用的任何设备上定位用户,您需要自己将这些设备的令牌与用户相关联。在上面链接的代码示例中,完成了:

firestore()
    .collection('users')
    .doc(userId)
    .update({
      tokens: firestore.FieldValue.arrayUnion(token),
    });

此代码将令牌保存到 Cloud Firestore,但它在任何数据库上的工作方式都相同:这会以某种方式将 FCM 令牌 (token) 与当前用户 (userId) 相关联允许每个用户存储多个令牌。然后,当您想向用户发送消息时,您可以查找该用户的令牌并调用 FCM 以发送到所有这些令牌。

另见:

  • Can we send custom push notifications to independent users using Firebase?