移动应用推送 SDK

更新时间:2019-02-01 10:53:21

概述

阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效、精确、实时的移动推送的同时,极大地降低了开发成本。让开发者最有效地与用户保持连接,从而提高用户活跃度、提高应用的留存率。

初始化

应用中注册和启动移动推送,在Application里添加初始化代码

PushServiceFactory.init(app);
    CloudPushService pushService = PushServiceFactory.getCloudPushService();
    // 设置安全图片的auth code
    pushService.setSecurityGuardAuthCode("114d");
    pushService.register(app, new CommonCallback() {
        @Override
        public void onSuccess(String response) {
            Log.d("aep-demo", "init cloudchannel success");
        }

        @Override
        public void onFailed(String errorCode, String errorMessage) {
            Log.d("aep-demo", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
        }
    });

创建消息接收 Receiver,继承自 com.alibaba.sdk.android.push.MessageReceiver,并在对应回调中添加业务处理逻辑,可参考以下代码:

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
            Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }
}

将该receiver添加到 AndroidManifest.xml

<!-- 消息接收监听器 (用户可自主扩展) -->
<receiver
    android:name=".MyMessageReceiver"
    android:exported="false"> <!-- 为保证receiver安全,建议设置不可导出,如需对其他应用开放可通过android:permission进行限制 -->
    <intent-filter>
        <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.alibaba.sdk.android.push.RECEIVE" />
    </intent-filter>
</receiver>

当接收到服务器下发的消息时,会触发 onNotification 方法

确认初始化正常

回调方法callback.onSuccess()被调用。以上文接入代码为例,logcat将会打印以下日志:

11-24 12:55:51.096  15235-15535/com.alibaba.xxxx D/YourApp﹕ init cloudchannel success

确认cloudchannel初始化正常,在logcat日志中:输入awcn关键字:

11-24 12:53:51.036  15235-15556/com.alibaba.xxxx E/awcn﹕ |[seq:AWCN1_1] AUTH httpStatusCode: 200
11-24 12:53:51.036  15235-15556/com.alibaba.xxxx E/awcn﹕ |[seq:AWCN1_1] status:AUTH_SUCC

确认DeviceId获取正常:在初始化成功后使用 cloudPushService.getDeviceId() 获取deviceId,应该能够成功获取。

使用方式

支持两种类型的消息下发,通知类型和消息类型

通知类型

服务器下发的通知,SDK 会自动处理,根据下发配置标题和内容自动弹出通知,无需添加业务代码。

消息类型

服务器下发的消息可以在初始化中创建的MyMessageReceiver 对应的回调方法中获得,如需弹出通知在此模式下需要自己根据下发的内容弹出通知;

账号的绑定和解绑

为了能够根据用户的id进行推送告警消息,需要在用户登入的时候进行deviceId和用户id进行绑定操作登出的时候进行解绑相关操作。具体可以参考如下代码

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(LoginBusiness.LOGIN_CHANGE_ACTION);
LocalBroadcastManager lb = LocalBroadcastManager.getInstance(app);
lb.registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (LoginBusiness.isLogin()) {
            PushManager.getInstance().bindUser();
        } else {
            PushManager.getInstance().unbindUser();
        }
    }
}, intentFilter);

混淆配置


-keepclasseswithmembernames class ** {
    native <methods>;
}

-keepattributes Signature

-keep class sun.misc.Unsafe { *; }

-keep class com.taobao.** {*;}

-keep class com.alibaba.** {*;}

-keep class com.alipay.** {*;}

-keep class com.ut.** {*;}

-keep class com.ta.** {*;}

-keep class anet.**{*;}

-keep class anetwork.**{*;}

-keep class org.android.spdy.**{*;}

-keep class org.android.agoo.**{*;}

-keep class android.os.**{*;}

-dontwarn com.taobao.**

-dontwarn com.alibaba.**

-dontwarn com.alipay.**

-dontwarn anet.**

-dontwarn org.android.spdy.**

-dontwarn org.android.agoo.**

-dontwarn anetwork.**

-dontwarn com.ut.**

-dontwarn com.ta.**

更多参考

更多内容请参考 https://help.aliyun.com/document_detail/51056.html?spm=a2c4g.11186623.6.623.11257fa8RbRfqu

results matching ""

    No results matching ""