1缝裤、JCenter接入
依賴jpush和jcore模塊屏轰,開啟廠商通道需依賴對(duì)應(yīng)模塊lib,并在極光后臺(tái)對(duì)應(yīng)的應(yīng)用設(shè)置中開啟廠商開關(guān)憋飞。
dependencies {
implementation 'cn.jiguang.sdk:jpush:3.3.6'
implementation 'cn.jiguang.sdk:jcore:2.1.4'
api 'cn.jiguang.sdk.plugin:xiaomi:3.3.6'
api 'cn.jiguang.sdk.plugin:huawei:3.3.6'
api 'cn.jiguang.sdk.plugin:meizu:3.3.6'
}
AndroidManifest中配置權(quán)限霎苗,添加必要組件。
<!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process參數(shù)將Service放在其他進(jìn)程中搀崭;android:enabled屬性不能是false -->
<!-- 這個(gè)是自定義Service叨粘,要繼承極光JCommonService,可以在更多手機(jī)平臺(tái)上使得推送通道保持的更穩(wěn)定 -->
<service android:name=".PushService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
<receiver android:name=".JPushCustomMessageReceiver">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<!--Required 顯示通知欄 -->
<category android:name="${applicationId}" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- Optional -->
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
JPushCustomMessageReceiver用戶自定義廣播接受者瘤睹,繼承JPushMessageReceiver升敲,在onMessage方法中用戶可以自己處理消息回調(diào)。
/**
* 收到自定義消息回調(diào)
*
* @param context Application
* @param customMessage Message
*/
@Override
public void onMessage(Context context, CustomMessage customMessage) {
super.onMessage(context, customMessage);
Log.i("jiguang", customMessage.extra);
HandleMessage.handleCustomMessage(context, customMessage);
}
最后在代碼中調(diào)用極光初始化Api轰传。
2驴党、廠商通道
通過集成廠商通道,即使應(yīng)用退出進(jìn)程也可以收到推送消息获茬,需要單獨(dú)去各廠商注冊(cè)應(yīng)用港庄,申請(qǐng)id倔既、key等值。
module的build.gradle中需要定義廠商渠道變量鹏氧。
manifestPlaceholders = ["authoritiesID" : applicationId,
XIAOMI_APPKEY : "MI-您的應(yīng)用對(duì)應(yīng)的小米的APPKEY",//小米平臺(tái)注冊(cè)的appkey
XIAOMI_APPID : "MI-您的應(yīng)用對(duì)應(yīng)的小米的APPID",//小米平臺(tái)注冊(cè)的appid
HUAWEI_APPID : "您的應(yīng)用對(duì)應(yīng)華為的appID",
MEIZU_APPKEY : "MZ-您的應(yīng)用對(duì)應(yīng)的魅族appkey",//魅族平臺(tái)注冊(cè)的appkey
MEIZU_APPID : "MZ-您的應(yīng)用對(duì)應(yīng)魅族的appid",//魅族平臺(tái)注冊(cè)的appid
]
注意小米渤涌、魅族的前綴,因?yàn)樾∶捉o的AppId是一個(gè)長(zhǎng)數(shù)字型字符把还,最終打包后的值會(huì)被gradle替換為一個(gè)錯(cuò)誤值实蓬。
3、推送消息
在極光后臺(tái)有兩只推送方式:通知和自定義消息吊履;而服務(wù)端則是通過Api推送安皱。
在極光后臺(tái)推送時(shí),可選擇通過channel艇炎、標(biāo)簽或者registeredId來精準(zhǔn)推送酌伊。
4、極光推送自定義鈴聲
通過極光后臺(tái)的自定義消息方式推送缀踪,在JPushCustomMessageReceiver的onMessage回調(diào)中居砖,創(chuàng)建自定義通知。
Android 26開始辜贵,創(chuàng)建通知需要使用NotificationChannel悯蝉,在NotificationManager#createNotificationChannel方法調(diào)用前归形,可通過NotificationChannel#setSound設(shè)置自定義鈴聲托慨。
此通道的名稱也將顯示在手機(jī)通知管理中,默認(rèn)鈴聲即為設(shè)置的自定義鈴聲暇榴。
// 自定義消息的通知使用此channel厚棵,自定義鈴聲
public static String CUSTOM_CHANNEL_ID = "custom_channel_id";
public static String CUSTOM_CHANNEL_NAME = "自定義推送";
/**
* 創(chuàng)建通知渠道
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private static void notificationChannel() {
NotificationManager manager = (NotificationManager) ZBApplication.getApplication().getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CUSTOM_CHANNEL_ID,
CUSTOM_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
Uri sound = Uri.parse("android.resource://" + ZBApplication.getApplication().getPackageName() + "/"+ R.raw.sound);
channel.setSound(sound, Notification.AUDIO_ATTRIBUTES_DEFAULT);
channel.enableLights(true);//顯示桌面紅點(diǎn)
channel.setLightColor(Color.GREEN);
channel.enableVibration(true);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
}
如果發(fā)現(xiàn)自定義鈴聲未生效,應(yīng)當(dāng)排查此channel的id是否在其他地方已經(jīng)注冊(cè)過了蔼紧。