我是靠谱客的博主 土豪蜡烛,最近开发中收集的这篇文章主要介绍android 点击通知栏打开activity,Android通知栏,点击唤醒APP,跳转到指定Activity,终极方案...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

这个功能基本上每个app都会需要,希望能帮到你。

1、接收消息:接收推送的消息,一般有两种:自己的推送服务和第三方推送服务。不管是哪一种,都会按照app的需求接收到不同类型的消息,然后在需要弹notification的地方通知。这是句废话

2、弹出notification:

此时,需要指定一个PendingIntent,如果用户在app内部或是点击手机home键退到后台,此时ActivityManager栈中是有Activity的,跳到指定的activity,再点击返回,仍然在app内部;如果用户是双击退出,finish掉了MainActivity,即此时的ActivityManager是空的,跳转到指定Activity,再点击返回,就回到了手机桌面,不在app内部了。所以,此时需要放app中的MainActivity在PendingIntent内。

然后,弹出notification:此时可以把存有跳转信息的msg信息传给MainTabActivity

Intent intent = new Intent(context, MainTabActivity.class);

intent.putExtra("PushMsg", msg);

PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

notification(msg.getMsgTitle(defTitle), msg.getMsgText(), pi);

注意:在使用PendingIntent.getActivity的时候,最后一个参数flag,千万别设置0,因为如果设置为0的话,在MainTabActivity的onNewIntent和onCreate中拿不到“PushMsg”信息。

最后,创建Notification,此时需要注意Android SDK26,必须创建NotificationChannel。

private void notification(String title, String content, PendingIntent intent) {

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel channel = NotificationUtils.getRPushNotificationChannel();

manager.createNotificationChannel(channel);

}

long when = System.currentTimeMillis();

Notification notification = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_R_PUSH_ID)

.setContentTitle(title)

.setContentText(content)

.setWhen(when)

.setSmallIcon(R.mipmap.ic_launcher_round)

.setContentIntent(intent)

.setAutoCancel(true)

.setPriority(NotificationCompat.PRIORITY_MAX)

.build();

manager.notify((int) when, notification);

}

public static NotificationChannel getPushNotificationChannel() {

// Android8.0及以上NotificationChannel机制

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

int importance = NotificationManager.IMPORTANCE_HIGH;

NotificationChannel mChannel = new NotificationChannel(CHANNEL_R_PUSH_ID, CHANNEL_R_PUSH_NAME, importance);

// 配置通知渠道的属性

mChannel.setDescription(CHANNEL_R_PUSH_DESC);

// 设置通知出现时的闪灯(如果 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});

return mChannel;

} else {

return null;

}

}

3、在MainTabActivity中处理跳转到指定Activity的事情。

如果MainActivity在ActivityManager中,点击通知会进入onNewIntent方法:此时直接在intent参数中获取传递的数据。

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

Logger.t("MainTabActivity").i("onNewIntent");

if (intent != null && intent.getExtras() != null) {

PushEntity pushEntity = (PushEntity) intent.getExtras().getSerializable("PushMsg");

startActivityForNotification(pushEntity);

}

}

如果ActivityManager是空的,点击通知会进入onCreate方法:此时需要用getIntent()方法获取传递的数据。

@Override

public void onCreate() {

Logger.t("MainTabActivity").i("getIntentData");

Intent intent = getIntent();

if (intent != null && intent.getExtras() != null) {

PushEntity pushEntity = (PushEntity) intent.getExtras().getSerializable("PushMsg");

//这个方法是做跳转到指定Activity用的,就不贴代码了

//一般是根据消息类型写死跳转

//或使用传递的数据,反射得到指定的class,再跳转

startActivityForNotification(pushEntity);

}

}

最后

以上就是土豪蜡烛为你收集整理的android 点击通知栏打开activity,Android通知栏,点击唤醒APP,跳转到指定Activity,终极方案...的全部内容,希望文章能够帮你解决android 点击通知栏打开activity,Android通知栏,点击唤醒APP,跳转到指定Activity,终极方案...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部