您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页GCM迁移到FCM

GCM迁移到FCM

来源:二三娱乐

2019年4月11日,Google将停用GCM服务器和客户端的API,因此需在此之前将GCM迁移到FCM。

迁移分为两种情况:

将GCM项目迁移到FCM

适用于GCM项目尚未集成Firebase、为该项目新建Firebase项目的情况。

迁移过程涉及到Firebase控制台、客户端、服务器三个部分。

迁移步骤如下:

监听新token

public class MyFirebaseMessagingService 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 called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // 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(token);
    }
    // [END on_new_token]
}

获取token

// Get token
// [START retrieve_current_token]
FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {
                Log.w(TAG, "getInstanceId failed", task.getException());
                return;
            }

            // Get new Instance ID token
            String token = task.getResult().getToken();
        }
    });
// [END retrieve_current_token]

将GCM项目迁移到(已有的)Firebase项目

适用于已有Firebase项目,将现有GCM项目添加至该Firebase项目的情况。

迁移步骤如下:

  1. 执行上一节的第2-5步。

  2. [如有] 取消tokens在GCM项目topics的订阅,并重新订阅到等效的Firebase项目的topics

  3. 使用旧server key向现有的GCM项目实例(旧tokens)发送消息,直到旧实例都升级为FCM实例

第2步需要服务器将GCM的token和FCM的token分开存储,客户端向服务器上报token的接口也需更改或增加参数以作区分。
发送推送时,GCM的token用户用原有GCM的server_key推送,FCM的token用户用新的FCM的server_key推送。

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务