Android 阿里雲推送集成指南

一、註冊阿里雲開發者賬號,創建應用獲取Appkey以及AppSecrent

二、在AndroidMainifest添加權限,還有一些其他權限(基本上你自己的項目已經寫了,我就不貼了)

<uses-permission android:name="android.permission.WRITE_SETTINGS"
 tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
 tools:ignore="ProtectedPermissions" />
android:allowBackup="true"在Application加上這個屬性。

三、在build.gradle中引入阿里雲推送庫(根據官方文檔的引入就一個的會有問題,親測這種是最保險的)

implementation 'com.aliyun.ams:alicloud-android-push:3.1.4@aar'
implementation 'com.aliyun.ams:alicloud-android-utils:1.1.3'
implementation 'com.aliyun.ams:alicloud-android-beacon:1.0.1'
implementation 'com.aliyun.ams:alicloud-android-ut:5.4.0'

設置ndk支持so包,abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'其實國內手機99%以上都是arm的,

就支持arm就可以了,這樣也能減小apk體積,因爲so的體積確實不小,每個項目都適用。

在gradle.properties加入android.useDeprecatedNdk=true

四、在AndroidMainifest中加入以下,阿里雲的配置

<meta-data android:name="com.alibaba.app.appkey" android:value="yourKey"/> <!-- 請填寫你自己的- appKey -->
<meta-data android:name="com.alibaba.app.appsecret" android:value="yourAppsecret"/> <!-- 請填寫你自己的appSecret -->
<!-- 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>
<!-- 消息接收監聽器 (用戶可自主擴展) -->
<receiver
 android:name=".receiver.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中初始化阿里雲推送

/**
 * 初始化雲推送通道
 * @param applicationContext
 */
private void initCloudChannel(Context applicationContext) {
    createNotificationChannel();
 PushServiceFactory.init(applicationContext);
 CloudPushService pushService = PushServiceFactory.getCloudPushService();
 pushService.register(applicationContext, new CommonCallback() {
        @Override
 public void onSuccess(String response) {
            String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
 Log.e(TAG,deviceId);
 Log.d(TAG, "init cloudchannel success");
 }
        @Override
 public void onFailed(String errorCode, String errorMessage) {
            Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
 }
    });

}
private void createNotificationChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 // 通知渠道的id
 String id = "1";//這個是與後臺約定好的,要不收不到,該方法主要是適配Android 8.0以上,避免接收不到通知
 // 用戶可以看到的通知渠道的名字.
 CharSequence name = "氫氪管家";
 // 用戶可以看到的通知渠道的描述
 String description = "管理氫氪車輛";
 int importance = NotificationManager.IMPORTANCE_HIGH;
 NotificationChannel mChannel = new NotificationChannel(id, name, importance);
 // 配置通知渠道的屬性
 mChannel.setDescription(description);
 // 設置通知出現時的閃燈(如果 android 設備支持的話)
 mChannel.enableLights(true);
 mChannel.setLightColor(Color.RED);
 // 設置通知出現時的震動(如果 android 設備支持的話)
 mChannel.enableVibration(true);
 mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
 //最後在notificationmanager中創建該通知渠道
 mNotificationManager.createNotificationChannel(mChannel);
 }
}

六、自定義MyMessageReceiver

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) {//當通知準確到達用戶的時候觸發
        ToastManage.s(context,title+"推送");
 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());
 ToastManage.s(context,"消息:"+cPushMessage.getTitle());
 }
    @Override
 public void onNotificationOpened(Context context, String title, String summary, String extraMap) {//當通知展開的時候觸發的操作
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
 ToastManage.s(context,"打開了通知:"+title);
 }
    @Override
 protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {//當通知被點擊了觸發的操作,並且沒有配置跳轉路徑
        ToastManage.s(context,"onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + 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) {//當用戶使用時接收到通知
        ToastManage.s(context,"onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + 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) {//通知被移除
        ToastManage.s(context,"通知被移除了");
 Log.e("MyMessageReceiver", "onNotificationRemoved");
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章