筆者最近在重構公司項目中的push模塊,之前某個業(yè)務會同時推一條通知欄消息和一條透傳消息阳啥,分別對應個推中onNotificationMessageArrived
和onReceiveMessageData
湃番。然而當點擊通知欄消息時统翩,個推在觸發(fā)onNotificationMessageClicked
后默認又會執(zhí)行onReceiveMessageData
常拓,導致還需要額外寫代碼去區(qū)分收到的是透傳消息還是通知欄點擊颈嚼,十分蛋疼未玻。本次重構修改為后端只推一條透傳消息灾而,由于個推的透傳沒有提供默認通知欄的方法,為保留之前的業(yè)務邏輯Android端需要在透傳中創(chuàng)建Notification并綁定通知的點擊事件扳剿。
要點
1.onReceivePassThroughMessage
中處理原來的透傳消息旁趟,并創(chuàng)建Notification
2.Notification的點擊事件綁定靜態(tài)廣播,收到廣播后執(zhí)行通知欄點擊方法
3.由于推送模塊是個library需要從app工程中獲取通知欄圖標資源文件
新增代碼
/**
* 個推透傳創(chuàng)建通知欄
*
* @param title
* @param subtitle
*/
private void addNotification(String title, String subtitle, MixPushMessage message) {
//顯示不重復通知
int requestCode = (int) System.currentTimeMillis();
Intent broadcastIntent = new Intent(this, GeTuiNotificationClickReceiver.class);
broadcastIntent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.
getBroadcast(this, requestCode, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(subtitle)
.setDefaults(Notification.DEFAULT_LIGHTS)
//.setVibrate(new long[]{0, 300, 300, 300})
//設置點擊通知跳轉(zhuǎn)頁面后庇绽,通知消失
.setAutoCancel(true)
.setContentIntent(pendingIntent);
//獲取app工程中的圖片資源
int logoId = getApplicationContext().getResources().getIdentifier(getIconName(getApplicationContext()), "mipmap",
getApplicationContext().getPackageName());
builder.setSmallIcon(logoId);
NotificationManager manager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
manager.notify(requestCode, builder.build());
}
上邊的代碼中值得一提的是pendingIntent 中的requestCode和notify()參數(shù)中的requestCode轻庆,前者用來區(qū)分是否是同一intent癣猾,如果寫死的話只會取最新的intent,后者者對應通知的id余爆,寫死的話重復使用同一條通知欄通知纷宇。
/**
* 獲取主工程mipmap下的資源文件名
*/
public static String getIconName(Context mContext) {
String value = "";
try {
ApplicationInfo appInfo = mContext.getPackageManager().
getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
value = appInfo.metaData.getString("OEM_ICON");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return value;
}
/**
* 靜態(tài)廣播接收器-通知欄點擊
*/
public class GeTuiNotificationClickReceiver extends BroadcastReceiver {
public GeTuiNotificationClickReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
GeTuiManager.sMixMessageProvider.onNotificationMessageClicked(context, (MixPushMessage) intent.getSerializableExtra("message"));
}
}
相關代碼
public class GeTuiManager implements MixPushManager {
public static MixMessageProvider sMixMessageProvider;
@Override
public String getPushName() {
return PushChannel.getui.name();
}
@Override
public void registerPush(Context context) {
PushManager.getInstance().initialize(context, null);
PushManager.getInstance().registerPushIntentService(context, GeTuiMessageIntentService.class);
}
@Override
public void unRegisterPush(Context context) {
PushManager.getInstance().stopService(context);
}
@Override
public void setMessageProvider(MixMessageProvider provider) {
sMixMessageProvider = provider;
}
}
public interface MixMessageProvider {
/**
* 透傳
*/
void onReceivePassThroughMessage(Context context, MixPushMessage message);
/**
* 通知欄消息點擊
*/
void onNotificationMessageClicked(Context context, MixPushMessage message);
/**
* 通知欄消息到達
*/
void onNotificationMessageArrived(Context context, MixPushMessage message);
/**
* 客戶端推送ID
*/
void onReceiveClientId(Context context, MixPushMessage message);
}
最后不要忘了在AndroidManifest.xml中注冊廣播!
<receiver
android:name=".receiver.GeTuiNotificationClickReceiver"
android:enabled="true"
android:exported="false"/>