一:效果图
二:实现步骤
去阿里云官方注册账号及随便认证一下,然后去控制台创建自己的应用,控制台——>产品与服务——>移动服务——>移动推送、
- 创建自己的项目(新建产品)、
- 新建成功后点击图标进入应用管理、
- 点击加号进行应用配置,输入软件名及项目包名、
- 点创建应用一直点下一步,完成后得到AppKey和AppSecret、
- 去下载jar文件和so文件,具体自己去找,我这需要的是三个jar一个v7的so文件,记得引用jar文件、
alicloud-android-push-sdk-3.0.12.jar
alicloud-android-ut-5.1.0.jar
utdid4all-1.1.5.3_proguard.jar
- 去build.gradle下配置,引用so文件及libs、
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'
})
- 在AndroidManifest文件中设置appKey,appSecret、
<!-- 请填写你自己的- appKey 阿里云推送-->
<meta-data android:name="com.alibaba.app.appkey" android:value="*******" />
<!-- 请填写你自己的appSecret 阿里云推送-->
<meta-data android:name="com.alibaba.app.appsecret" android:value="******" />
- 添加相关推送权限、
<!-- 阿里云推送相关权限 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- SDK相关组件注册
<!-- Push SDK 相关组件,required-->
<!-- 消息接收服务 -->
<service
android:name="com.alibaba.sdk.android.push.MsgService"
android:exported="false">
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.NOTIFY_ACTION" />
</intent-filter>
</service>
<service android:name="com.alibaba.sdk.android.push.channel.CheckService"
android:process=":channel">
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.CHECK_SERVICE" />
</intent-filter>
</service>
<service android:name="com.taobao.accs.ChannelService"
android:exported="true" android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.SERVICE"/>
</intent-filter>
</service>
<service
android:name="com.taobao.accs.ChannelService$KernelService"
android:exported="false"
android:process=":channel" >
</service>
<service android:name="com.taobao.accs.data.MsgDistributeService"
android:exported="true">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<receiver android:name="com.taobao.accs.EventReceiver"
android:process=":channel">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
<receiver android:name="com.taobao.accs.ServiceReceiver"
android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.COMMAND"/>
</intent-filter>
<intent-filter>
<action android:name="com.taobao.accs.intent.action.START_FROM_AGOO"/>
</intent-filter>
</receiver>
<service android:name="org.android.agoo.accs.AgooService"
android:exported="true" >
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<service android:name="com.alibaba.sdk.android.push.AliyunPushIntentService"
android:exported="true"
>
<intent-filter>
<action android:name="org.agoo.android.intent.action.RECEIVE" />
</intent-filter>
</service>
<receiver
android:name="com.taobao.agoo.AgooCommondReceiver"
android:process=":channel"
android:exported="true" >
<intent-filter>
<action android:name="${applicationId}.intent.action.COMMAND" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service
android:name="com.alibaba.sdk.android.push.channel.TaobaoRecvService"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="org.android.agoo.client.MessageReceiverService" />
</intent-filter>
</service>
<!-- V3.0.12及以上版本需配置 -->
<service
android:name="com.taobao.accs.internal.AccsJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":channel"/>
<!-- V3.0.7及以上版本需配置 -->
<service android:name="com.alibaba.sdk.android.push.channel.KeepChannelService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":channel" />
<receiver android:name="com.alibaba.sdk.android.push.SystemEventReceiver"
android:process=":channel">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
<!-- V3.0.9及以上版本需配置 -->
<activity
android:name="com.alibaba.sdk.android.push.keeplive.PushExtActivity"
android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:process=":channel"
>
</activity>
自定义消息监听器
/**
* 作 者 : 浪哥
* 功 能 : 消息接收监听器
* 所属模块 :阿里云推送
* 创建时间 : 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<String, String> extraMap) {
// TODO 处理推送通知
Log.e(REC_TAG, "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
if ( null != extraMap ) {
for (Map.Entry<String, String> 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<String, String> 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");
}
注册自定义的消息监听器
<!-- 消息接收监听器 (用户可自主扩展) -->
<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>
自定义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);
}
});