第一步: 注册自定义推送消息处理
NIMPushClient.registerMixPushMessageHandler(DemoMixPushMessageHandler())
第二步
public class DemoMixPushMessageHandler implements MixPushMessageHandler {
@Override
public boolean onNotificationClicked(Context context, Map<String, String> payload) {
boolean existMainActivity = isProcessExist(context,android.os.Process.myPid());
Intent notifyIntent;
if (existMainActivity) {
if (context instanceof MixPushActivity) {
notifyIntent = new Intent(context, MainActivity.class);
}else {
notifyIntent = new Intent();
notifyIntent.setComponent(initLaunchComponent(context));
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.setAction(Intent.ACTION_VIEW);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
}
}else {
notifyIntent = new Intent(context, SplashActivity.class);
}
context.startActivity(notifyIntent);
return true;
}
private ComponentName initLaunchComponent(Context context) {
ComponentName launchComponent;
StatusBarNotificationConfig config = DemoCache.getNotificationConfig();
Class<? extends Activity> entrance = config.notificationEntrance;
if (entrance == null) {
launchComponent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
} else {
launchComponent = new ComponentName(context, entrance);
}
return launchComponent;
}
// 将音视频通知 Notification 缓存,清除所有通知后再次弹出 Notification,避免清除之后找不到打开正在进行音视频通话界面的入口
@Override
public boolean cleanMixPushNotifications(int pushType) {
Context context = DemoCache.getContext();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.cancelAll();
}
return true;
}
/**
* 判断进程是否存活
*/
public static boolean isProcessExist(Context context, int pid) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> lists ;
if (am != null) {
lists = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : lists) {
if (appProcess.pid == pid) {
LogUtils.e("==================isProcessExist========333333");
return true;
}
}
}
return false;
}
}
第三步:
自定义该应用初始的 StatusBarNotificationConfig
StatusBarNotificationConfig config = new StatusBarNotificationConfig();
// 点击通知需要跳转到的界面
config.notificationEntrance = MixPushActivity.class;
MixPushActivity 里面
public class MixPushActivity extends Activity {
private static final String TAG = "MixPushActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
parseIntent();
}
void parseIntent() {
Intent intent = getIntent();
if (intent == null) {
return;
}
ArrayList<IMMessage> msgListExtra = (ArrayList<IMMessage>) intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
new DemoMixPushMessageHandler().onNotificationClicked(MixPushActivity.this,null);
}
@Override
protected void onStop() {
super.onStop();
finish();
}
}
oppo 通知栏点击事件
华为通知栏点击事件请看
Android 集成云信推送 (三)踩过的那些坑