4

I want to retrieve my token and i'm doing exactly as many examples says On FirebaseMessagingService i have this

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

And i got the error

Method does not override method from it's superclass

and of course the super.onNewToken(token) has the error

cannot resolve method

On my mainactivity i have this inside OnCreate()

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
     @Override
     public void onSuccess(InstanceIdResult instanceIdResult) {          
           String newToken = instanceIdResult.getToken();    
     }
 });

And the errors i get are:

Cannot resolve getInstanceId()

Cannot resolve InstanceIdResult

cannot resolve getToken()

and Method does not override method from it's

superclass

Update

Class declaration

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    private NotificationUtils notificationUtils;


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

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getNotification().getBody());
        }

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }

Dependencies

implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

Update v2

public class FirebaseMessagingService extends com.google.firebase.iid.zzb {
    private static final java.util.Queue<java.lang.String> zzoma;

    public FirebaseMessagingService() { /* compiled code */ }

    @android.support.annotation.WorkerThread
    public void onMessageReceived(com.google.firebase.messaging.RemoteMessage remoteMessage) { /* compiled code */ }

    @android.support.annotation.WorkerThread
    public void onDeletedMessages() { /* compiled code */ }

    @android.support.annotation.WorkerThread
    public void onMessageSent(java.lang.String s) { /* compiled code */ }

    @android.support.annotation.WorkerThread
    public void onSendError(java.lang.String s, java.lang.Exception e) { /* compiled code */ }

    @com.google.android.gms.common.internal.Hide
    protected final android.content.Intent zzp(android.content.Intent intent) { /* compiled code */ }

    @com.google.android.gms.common.internal.Hide
    public final boolean zzq(android.content.Intent intent) { /* compiled code */ }

    @com.google.android.gms.common.internal.Hide
    public final void handleIntent(android.content.Intent intent) { /* compiled code */ }

    static void zzr(android.os.Bundle bundle) { /* compiled code */ }

    static boolean zzal(android.os.Bundle bundle) { /* compiled code */ }
Alex
  • 1,816
  • 5
  • 23
  • 39

1 Answers1

1

build.gradle (app):

implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

extend class:

package /*package name*/;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;

public class MyFcmListenerService extends FirebaseMessagingService {
    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is also called
     * when the Instance ID token is initially generated, so this is where
     * you retrieve the token.
     */
    @Override
    public void onNewToken(String token) {
        Log.d("TAG", "New token: " + token);
        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(token); //As I understand it, you need to implement it yourself.
    }
}

in tag in AndroidManifest.xml:

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

Add to your activity where you want to get a token:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this,  new OnSuccessListener<InstanceIdResult>() {
       @Override
       public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
       }
    });
Magic Max
  • 21
  • 3
  • There is an error with `getApplication ()` for the code below. Do you know how to handle it? `private void sendRegistrationToServer(@Nullable String token) { if (token != null) { ((SubApp) getApplication()).getRepository().registerInstanceId(token); // No need to unregister the previous Instance ID token because the server // automatically removes invalidated tokens. } }` – Namikaze Minato May 03 '20 at 03:50
  • And still: `Method doesn't override method from its superclass`. I am working on classytaxijava that I get these errors – Namikaze Minato May 03 '20 at 03:53