android - Firebase onTokenRefresh() 未被调用

标签 android firebase firebase-cloud-messaging firebase-notifications

在我的 MainActivity 在我的日志中,我可以看到使用 FirebaseInstanceId.getInstance().getToken() 的 token 并显示生成的 token 。但似乎在我的 MyFirebaseInstanceIDService 延伸到 FirebaseInstanceIdService 的地方,onTokenRefresh() 没有被调用,在这个函数中它被说 token 最初是在此处生成的。我需要调用 sendRegistrationToServer() 这就是为什么我想知道它为什么不进入 onTokenRefresh() 的原因。

这是我的代码

public class MyFirebaseInstanceIDService  extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }
}

最佳答案

onTokenRefresh in FirebaseInstanceIdService is only called when a new token is generated. If your app was previously installed and generated a token then onTokenRefresh would not be called. Try uninstalling and reinstalling the app to force the generation of a new token, this would cause onTokenRefresh to be called.

还要确保您的 FirebaseInstanceIdService 在您的 AndroidManifest.xml 中正确定义

在您的 list 文件中。

 <service
        android:name="com.bnt.etailers.fcm.MyFireBaseInstanceIDService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <service
        android:name="com.bnt.etailers.fcm.GCMNotificationIntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

FirebaseInstanceIdService 类

public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {


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

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    if (refreshedToken!=null) {
        SettingPreferences.setStringValueInPref(this, SettingPreferences.REG_ID, refreshedToken);
    }
    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]

/**
 * Persist token to third-party servers.
 *
 * 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.
}}

FirebaseMessagingService 类。

public class GCMNotificationIntentService extends FirebaseMessagingService {
// Sets an ID for the notification, so it can be updated


public GCMNotificationIntentService() {
    super();
}


@Override
public void onMessageReceived(RemoteMessage message) {

}}

关于android - Firebase onTokenRefresh() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451395/

相关文章:

android - 如何动态地将 ImageView 添加到 View

javascript - AngularFire 0.5.0 $on 'change' 事件未正确触发

android - webmethod 不调用 firebase 通知 onMessageReceived 方法

使用 FCM 的 Angular 推送通知

android - 如何使用android和OpenCV将叠加层附加到检测到的对象

android - RxJava : fromIterable is not emitting all the items

android - VideoView - MediaController 未显示

firebase - 服务器到服务器身份验证失败 - firebase - 对话流 - google 上的操作 - 用户实体 v2

ios - 将结果从 Swift 数组传递到 Segue 到描述

android - android中的Firebase计划通知