Firebase(FCM)如何获取令牌

新手上路,请多包涵

这是我第一次使用 FCM。

我从 firebase/quickstart-android 下载了一个示例,然后安装了 FCM Quickstart。但我无法从日志中获取任何令牌,甚至点击应用程序中的 LOG TOKEN 按钮

然后我尝试使用 Firebase 控制台发送消息并设置为目标我的应用程序包名称。我收到了传入的消息。

我想知道FCM可以用吗?GCM一切正常。

解决方案:

因为我不是安卓开发者,只是后端开发者。所以我需要一些时间来解决它。在我看来,示例应用程序中存在一些错误。

在此处输入图像描述

代码:

注册意图服务.java

 public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.i(TAG, "FCM Registration Token: " + token);
    }
}

MyFirebaseInstanceIDService.java

 public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @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);
//
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     * <p>
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

在 MainActivity.java 中添加它。

  Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);

完成上述操作后,您就得到了 Logcat 中的 Token。但最后,我找到了一种 方便的方法 来获得它。 只需使用调试模式安装示例应用程序,即可在第一次安装时获取令牌。

但是不知道为什么安装的时候不能打印日志。可能与移动系统有关。

然后为什么我无法收到通知。 FirebaseMessagingService.onMessageReceived 没有调用 sendNotification

原文由 wyx 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
1 个回答

如果您使用上面的 ‘com.google.firebase:firebase-messaging:22.0.0’

用 Kotlin 方式获取 FCM Token (Force):

 FirebaseMessaging.getInstance().token.addOnCompleteListener { task: Task<String> ->
   if (!task.isSuccessful) {
     return@addOnCompleteListener
   }

   pushToken = task.result
   Log.i("PUSH_TOKEN", "pushToken: $pushToken")
}

您可以通过 onNewToken 方法获取令牌。 (这个类必须实现)

 class PushNotificationService: FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        Log.e("PUSH_TOKEN", "Refreshed firebase token: $token")
    }
}

您需要将 PushNotificationService 添加到 AndroidManifest.xml

 <service
  android:name=".PushNotificationService"
  android:exported="false"
  tools:ignore="Instantiatable">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
</service>

原文由 Emefar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题