关于android:重写onNewToken并从Firebase错误中获取令牌

Overriding onNewToken and getting tokens from firebase errors

我想找回我的令牌,而我正在做很多示例
在FirebaseMessagingService上,我有这个

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

我得到了错误

Method does not override method from it's superclass

当然super.onNewToken(token)有错误

cannot resolve method

在我的mainactivity上,我在OnCreate()里面有这个

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

我得到的错误是:

Cannot resolve getInstanceId()

无法解析InstanceIdResult

不能
解析getToken()

并且Method不会覆盖它的方法

superclass

更新资料

类声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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());
            }
        }
    }

依存关系

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

更新v2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 */ }


build.gradle(应用程序):

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

扩展类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
    }
}

在AndroidManifest.xml中的标记中:

1
2
3
4
5
6
<service
    android:name=".MyFcmListenerService">
    <intent-filter>
         
    </intent-filter>
</service>

在您想要获得令牌的活动中添加:

1
2
3
4
5
6
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this,  new OnSuccessListener<InstanceIdResult>() {
       @Override
       public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
       }
    });