FCM令牌无效

时间:2016-10-26 05:44:29

标签: android firebase firebase-cloud-messaging

有时我得到一个相当短的FCM令牌,当用于通过服务器发送通知时会给出这个输出:

{   "multicast_id":7440720337322140527,
    "success":0,
    "failure":1,
    "canonical_ids":0,
    "results":[
        {"error":"InvalidRegistration"}
    ]
}

尝试1(Notification Push Succeeds):

ehLB538elk8:APA91bFAJ97yoWe9eUhG1x67fL_3w0ae6TxNLkm3Fp_l3LStYCdQhmZT1CPqzrBmIEDhXpUvf8v8lURtWpF6-qYVwU2kDILi9IvUM42McyQJpEbXKArFkk7--kMBQ8dFi0YlWmVOAKZX

尝试2(通知推送失败):

6854290a0a4bc01854786127189641e8441b03f6

有人可以指导我如何处理令牌的这种不确定性。

添加令牌检索代码:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    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) {
    Context appContext = getApplicationContext();
    String deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);

    if (appContext != null)
    {
        SharedPreferences sharedPreferences = this.getSharedPreferences("sharedprefs", MODE_PRIVATE);

        if (sharedPreferences != null)
        {
            sharedPreferences.edit().putString("cdToken", token).apply();

            try {
                Profile profile = JSON.std.beanFrom(Profile.class, sharedPreferences.getString("user_id", null));

                HashMap<String, String> params = (HashMap<String, String>) Utils.GetDeviceInfo(getApplicationContext());

                params.put(getApplicationContext().getString(R.string.USER_DEVICE_ID), deviceId);
                params.put(getApplicationContext().getString(R.string.USER_FCM_REGISTRATION_ID), profile.getToken());
                params.put(getApplicationContext().getString(R.string.USER_APP_SOURCE), String.valueOf(Constants.SOURCE_COLLEGE_DEKHO_APP));

                DeviceFcmRegistrationTask applyTask = new DeviceFcmRegistrationTask(params, profile.getToken());
                applyTask.execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

在正确的FCM令牌的情况下记录日志:

10-25 23:03:30.180 32045-32045/com.collegedekho.app D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
10-25 23:03:30.198 32045-32045/com.collegedekho.app D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
10-25 23:03:30.273 32045-32045/com.collegedekho.app I/FA: To enable faster debug mode event logging run:
                                                        adb shell setprop firebase.analytics.debug-mode com.collegedekho.app
10-25 23:03:30.305 32045-32045/com.collegedekho.app I/FirebaseInitProvider: FirebaseApp initialization successful
10-25 23:03:32.514 32045-32451/com.collegedekho.app D/MyFirebaseIIDService: Refreshed token: ffxAeD8ZD0Q:APA91bEWOZGQfYopSO6ymsC6cBLpcfZTJYQ2rn5EkbVyrvnd4CRXHDUzdF4p6A_0yqFta14cN038BbjL8BIX6ZKCKa-vR8guZVuoCVJdRm5vyWyU5nsP_jqkN_8I9f8GjKxEX-ueAhcO
10-25 23:03:32.540 32045-32451/com.collegedekho.app D/FirebaseInstanceId: topic sync succeeded

如果FCM令牌不正确,将发布日志。

0 个答案:

没有答案