android - 如果用户拥有多个设备,如何在我的服务器上存储 firebase token

标签 android firebase firebase-cloud-messaging

我正在使用这个众所周知的代码在我的服务器上保存新的 Firebase token (来自: Retrieve the current registration token ):

@Override
public void onTokenRefresh() {

    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
}

问题是,如果用户在其他设备上安装该应用程序,则会执行相同的代码,并且旧的 Firebase token 将被新的 token 覆盖。

所以我必须区分设备,但我该怎么做呢? Device name肯定不够独特。

编辑:不幸的是,鲍勃·斯奈德的建议没有奏效。 FirebaseInstanceId#getId()如果我删除应用程序数据(缓存)或重新安装应用程序,总是会有所不同。

最佳答案

token 对于每台设备都是唯一的,并且包含应用实例 ID。 11 个字符的实例 ID,与 FirebaseInstanceId.getInstance().getId() 返回的 ID 相同, 首先出现,然后是冒号,以及标记的其余部分。示例:

    eiURDSe_P4q:APA91bFvtCzK...LRpzvQOSdNKioklO

您可以使用实例 ID 作为唯一 key 来将 token 存储在服务器上。

关于android - 如果用户拥有多个设备,如何在我的服务器上存储 firebase token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172532/

相关文章:

android - 如果帐户在 Firebase Android 的初始注册期间已与谷歌关联,请使用 Facebook 登录

php - TestFlight 中未收到 iOS FCM 推送通知

java - Firebase onChildRemoved() 和 removeValue() 处理时间敏感操作的问题

android - RecyclerView 适合屏幕时不要折叠 Toolbar

java - DialogFragment 不显示消息

firebase - 在 Firebase 云存储中加载预览时出错

发送 OneSignal 推送通知时 Android 应用程序崩溃

node.js - 在云函数上创建消息主题

push-notification - Firebase 通知无法使用 FCM 服务正常工作

java - 从计时器函数内部更改 View