实施推送通知 Firebase

Implement push notification firebase

我正在使用此页面中的代码获取令牌 https://firebase.google.com/docs/cloud-messaging/android/client,但我在特定行 String msg = getString(R.string.msg_token_fmt, token); 上收到 msg_token_fmt 错误,提示无法解析符号。我已完成本教程中的所有步骤,但出现此错误。

这是代码

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();
        }
    });

我错过了什么?

该代码仅作为示例提供。如果您不打算使用应用程序中的字符串资源来格式化 token 字符串,那么您根本不需要该行代码。收到后用 token 做任何你想做的事。通常,您将其发送到后端,以便使用 FCM 发送 API.

的消息将令牌用于定位此设备