简体   繁体   English

如何获取用于推送通知的 Android 设备令牌?

[英]How to get Android device token for push notifications?

We have been through various Stackoverflow questions and answers, and looked through documentation and articles but all the approaches we've tried such as FireBaseInstanceId and extending the FirebaseMessagingService all result in an empty token.我们经历了各种 Stackoverflow 问题和答案,并查看了文档和文章,但我们尝试过的所有方法(例如 FireBaseInstanceId 和扩展 FirebaseMessagingService)都会导致一个空令牌。

It is not clear how we get the token for the user's device, for push notification purposes.目前尚不清楚我们如何获取用户设备的令牌以用于推送通知。

Could someone please clarify how in 2021 we can get the user's device token?有人可以澄清一下我们如何在 2021 年获得用户的设备令牌吗?

Thanks谢谢

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.e("newToken", token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    }

}

Add this in Android manifest file:在 Android 清单文件中添加:

<service
        android:name=".MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

Whenever your Application is installed first time and open, MyFirebaseMessagingService created and onNewToken(String token) method called and token generated which is your Device Token or FCM Token.每当您的应用程序首次安装并打开时,都会创建 MyFirebaseMessagingService 并调用 onNewToken(String token) 方法并生成令牌,即您的设备令牌或 FCM 令牌。

from this way you can get the unique device id通过这种方式,您可以获得唯一的设备ID

TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) getSystemService(Context.
                TELEPHONY_SERVICE);

getDeviceId() will returns the unique device ID. getDeviceId()将返回唯一的设备 ID。

String deviceId = telephonyManager.getDeviceId();

By the way this needs permission of android.permission.READ_PHONE_STATE顺便说一句,这需要android.permission.READ_PHONE_STATE的许可

FireBaseInstanceId was deprecated and documentation say to use FirebaseMessaging for retrieve FCM token. FireBaseInstanceId 已被弃用, 文档说使用 FirebaseMessaging 来检索 FCM 令牌。 In particular the method getToken() works in this way:特别是getToken()方法以这种方式工作:

Returns the FCM registration token for this Firebase project. This creates a Firebase Installations ID, if one does not exist

So, in order to generate token, I don't think is mandatory to extends FirebaseMessagingService, just call it like so wherever you want所以,为了生成令牌,我认为扩展 FirebaseMessagingService 不是强制性的,只要你想像这样调用它

FirebaseMessaging.getInstance().token.addOnCompleteListener {
        if (!it.isSuccessful) {
            return@addOnCompleteListener
        }
        val token = it.result //this is the token retrieved
    }

Once you have a sample push notification project working, then the Java code is:一旦你有一个示例推送通知项目工作,那么 Java 代码是:

FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
          if (!task.isSuccessful()) {
            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
            return;
          }

          // Get new FCM registration token
          String token = task.getResult();

          // Log and toast
          String msg = getString(R.string.msg_token_fmt, token);
          Log.d(TAG, msg);
          Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

Source=https://firebase.google.com/docs/cloud-messaging/android/client来源=https://firebase.google.com/docs/cloud-messaging/android/client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM