Search code examples
androidfirebasefirebase-cloud-messaging

Implement push notification firebase


I am using the code from this page to get the token https://firebase.google.com/docs/cloud-messaging/android/client but i am getting an error on this specific line String msg = getString(R.string.msg_token_fmt, token); for msg_token_fmt saying can not resolve symbol. I have done all steps in this tutorial but i get this error.

This is the code

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

What am i missing?


Solution

  • That code is just provided as an example. If you don't intend to format the token string with a string resource from your app, then you don't need that line of code at all. Just do whatever you want with token after you receive it. Typically you send it to your backend so that the token can be used to target this device with messages using the FCM send API.