一:一般最关注的就是结果了,先上效果图、
二:实现步骤
alicloud-android-push-sdk-3.0.12.jar
alicloud-android-ut-5.1.0.jar
utdid4all-1.1.5.3_proguard.jar
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
自定义消息监听器
/**
* 作 者 : 浪哥
* 功 能 : 消息接收监听器
* 所属模块 :阿里云推送
* 创建时间 : 2018/10/10
* 功能描述 : 自定义消息接收器便于做消息处理
*/
public class CgMessageReceiver extends MessageReceiver{
// 消息接收部分的LOG_TAG、
public static final String REC_TAG = "Langge";
/**
* 处理推送通知
*
* @param context
* @param title
* @param summary
* @param extraMap
*/
@Override
public void onNotification(Context context, String title, String summary, Map extraMap) {
// TODO 处理推送通知
Log.e(REC_TAG, "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
if ( null != extraMap ) {
for (Map.Entry entry : extraMap.entrySet()) {
Log.i(REC_TAG,"@Get diy param : Key=" + entry.getKey() + " , Value=" + entry.getValue());
}
} else {
Log.i(REC_TAG,"@收到通知 && 自定义消息为空");
}
}
/**
* 推送消息的回调方法
*
* @param context
* @param cPushMessage
*/
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
Log.e(REC_TAG, "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
}
/**
* 从通知栏打开通知的扩展处理
*
* @param context
* @param title
* @param summary
* @param extraMap
*/
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
Log.e(REC_TAG, "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
/**
* 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened
*
* @param context
* @param title
* @param summary
* @param extraMap
*/
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
Log.e(REC_TAG, "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
/**
* 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html#h3-3-4-basiccustompushnotification-api
*
* @param context
* @param title
* @param summary
* @param extraMap
* @param openType
* @param openActivity
* @param openUrl
*/
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) {
Log.e(REC_TAG, "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
}
/**
* 通知删除回调
*
* @param context
* @param messageId
*/
@Override
protected void onNotificationRemoved(Context context, String messageId) {
Log.e(REC_TAG, "onNotificationRemoved");
}
注册自定义的消息监听器
自定义Application初始化通道,不能在activity中初始化
/**
* 作 者 : 浪哥
* 功 能 : 自定义Application
* 所属模块 :阿里云推送
* 创建时间 : 2018/10/10
* 功能描述 : 自定义Application初始化通道等操作
*/
public class MainApplication extends Application{
private static final String TAG = "langge";
@Override
public void onCreate() {
super.onCreate();
initCloudChannel(this);
}
/**
* 初始化云推送通道
*
* @param applicationContext
*/
private void initCloudChannel(Context applicationContext) {
PushServiceFactory.init(applicationContext);
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.register(applicationContext, new CommonCallback() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "init cloudchannel success");
}
@Override
public void onFailed(String errorCode, String errorMessage) {
Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
}
});
}
}
PushServiceFactory.getCloudPushService().bindAccount(jsondata.getString("userId"), new CommonCallback() {//绑定账号--->用户ID
@Override
public void onSuccess(String s) {
Log.d(TAG, "阿里推送绑定账号成功" + s + " ");
}
@Override
public void onFailed(String s, String s1) {
Log.d(TAG, "阿里推送绑定账号失败 " + s + " " + s1);
}
});
PushServiceFactory.getCloudPushService().addAlias(departmentId, new CommonCallback() {//绑定别名--->组织ID
@Override
public void onSuccess(String s) {
Log.d(TAG, "阿里推送绑定组织ID成功" + s + " ID为: "+departmentId);
}
@Override
public void onFailed(String s, String s1) {
Log.d(TAG, "阿里推送绑定组织ID失败 " + s + " " + s1);
}
});
//解绑
PushServiceFactory.getCloudPushService().bindAccount(SpUtil.get(ConstantUtil.ID, ""), new CommonCallback() {//绑定账号--->用户ID
@Override
public void onSuccess(String s) {
Log.d(TAG, "阿里推送绑定账号成功" + s + " ");
}
@Override
public void onFailed(String s, String s1) {
Log.d(TAG, "阿里推送绑定账号失败 " + s + " " + s1);
}
});
PushServiceFactory.getCloudPushService().removeAlias(SpUtil.get(ConstantUtil.ZZID, ""), new CommonCallback() {//解绑别名--->组织ID
@Override
public void onSuccess(String s) {
Log.d(TAG, "阿里推送解绑组织ID成功" + s + " ID为: " + SpUtil.get(ConstantUtil.ZZID, ""));
}
@Override
public void onFailed(String s, String s1) {
Log.d(TAG, "阿里推送解绑组织ID失败 " + s + " " + s1);
}
});
----------------- 完事,感谢阅读,写得不好不接受反驳、
-----------------demo地址:https://download.csdn.net/download/android_cll/10710614