移动应用推送 SDK

更新时间:2019-06-21 18:43:54

概述

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

初始化

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

PushManager.getInstance().initPush(app); // app => application

创建消息接收 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 会自动处理,根据下发配置标题和内容自动弹出通知,无需在MyMessageRecevier中添加业务代码。

消息类型

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

混淆配置


-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.**



告警功能

告警功能依赖设备和用户绑定,SDK 在初始化后需要实现用户和设备的绑定,并监听用户登出时候的调用解绑

绑定

PushManager.getInstance().bindUser();

解绑

PushManager.getInstance().unbindUser();

语言切换支持(国际化支持)

使用API通道sdk切换语言之后,重新实现 deviceid 和用户体系绑定(如上所述),即可切换语言,目前可以支持 中文、 英文、法文、德文、日文、韩文、西班牙文、俄文八种语言。

绑定相关代码实现:

PushManager.getInstance().bindUser();

更多功能

请参见 阿里云移动推送

常见错误处理

请参见错误处理

results matching ""

    No results matching ""