文档

普通手机APP如何初始化移动推送SDK

更新时间:

问题描述

普通手机APP如何初始化移动推送SDK。

解决方案

  1. maven { url 'https://maven.aliyun.com/nexus/content/repositories/releases/' }

  2. implementation "com.aliyun.ams:alicloud-android-push:3.8.7" // 以3.8.7版本为例

  3. <application android:name="MainApplication"> <meta-data android:name="com.alibaba.app.appkey" android:value="*****"/> <!-- 请填写你自己的- appKey --> <meta-data android:name="com.alibaba.app.appsecret" android:value="****"/> <!-- 请填写你自己的appSecret --> </application>

  4. MainApplicationonCreate()方法中调用initCloudChannel(),且保证调用时不要判断主进程(initCloudChannel()的上一层直接是onCreate(),没有其他)。

  5. public class MainApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            initCloudChannel(this);
        }
    }
    private void initCloudChannel(Context context) {
        PushServiceFactory.init(context);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.setLogLevel(CloudPushService.LOG_DEBUG); //仅适用于Debug包,正式包不需要此行
        pushService.register(context, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.i(TAG, "initCloudChannel success " + pushService.getDeviceId());
            }
    
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.w(TAG, "initCloudChannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
        // 获取设备id
        String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
        Log.i(TAG, "deviceId==" + deviceId);
    }

初始化注册成功标志

initCloudChannel success 并且 “deviceId==”也取到了值。注意:getDeviceId()必须在register成功后才能取到值。

适用于

  • 移动推送

  • 本页导读 (0)
文档反馈