笔者最近在重构公司项目中的push模块,之前某个业务会同时推一条通知栏消息和一条透传消息,分别对应个推中onNotificationMessageArrived
和onReceiveMessageData
。然而当点击通知栏消息时,个推在触发onNotificationMessageClicked
后默认又会执行onReceiveMessageData
,导致还需要额外写代码去区分收到的是透传消息还是通知栏点击,十分蛋疼。本次重构修改为后端只推一条透传消息,由于个推的透传没有提供默认通知栏的方法,为保留之前的业务逻辑Android端需要在透传中创建Notification并绑定通知的点击事件。
要点
1.onReceivePassThroughMessage
中处理原来的透传消息,并创建Notification
2.Notification的点击事件绑定静态广播,收到广播后执行通知栏点击方法
3.由于推送模块是个library需要从app工程中获取通知栏图标资源文件
新增代码
/**
* 个推透传创建通知栏
*
* @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})
//设置点击通知跳转页面后,通知消失
.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()参数中的requestCode,前者用来区分是否是同一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;
}
/**
* 静态广播接收器-通知栏点击
*/
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"/>