Android监听消息通知栏点击事件

使用BroadCastReceiver

1 新建一个NotificationClickReceiver 类,并且在清单文件中注册!!

public class NotificationClickReceiver extends BroadcastReceiver {

    @Override
public void onReceive(Context context, Intent intent) {
//todo 跳转之前要处理的逻辑
Log.i("TAG", "userClick:我被点击啦!!! ");
Intent newIntent = new Intent(context, Main2Activity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
}
}

在清单文件中注册

 <receiver
android:name=".NotificationClickReceiver">
</receiver>

在你需要创建通知栏的地方

NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.ic_launcher); //设置图标
builder1.setTicker("显示第二个通知");
builder1.setContentTitle("通知"); //设置标题
builder1.setContentText("点击查看详细内容"); //消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失
Intent intent =new Intent (MainActivity.this,NotificationClickReceiver.class);
PendingIntent pendingIntent =PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
notificationManager.notify(124, notification1); // 通过通知管理器发送通知

如果需要携带什么参数就在这里的intent包裹即可,NotificationClickReceiver可以接收到发送过来的intent

兼容Android 8及以上

// 版本升级通知框
NotificationManager notificationManager = (NotificationManager) MapActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.Builder(MapActivity.this);
// 通知框兼容 android 8 及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("11212313131", "NotificationName", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
builder1.setChannelId("123456");
}
builder1.setSmallIcon(R.mipmap.touxiang); //设置图标
builder1.setContentTitle("这是一个通知"); //设置标题
builder1.setContentText("这是消息内容"); //消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失
Intent intent = new Intent(Activity.this, NotificationClickReceiver.class);
intent.putExtra("url","www.baidu.com");
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
notificationManager.notify(124, notification1); // 通过通知管理器发送通知
public class NotificationClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getStringExtra("url");
Uri uri = Uri.parse(url);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(i);
}
}

Android监听消息通知栏点击事件的更多相关文章

  1. Android 监听按钮的点击事件

    onClick事件1.Button和ImageButton都拥有一个onClick事件 通过自身的.setOnClickListener(OnClickListener)方法添加点击事件2.所有的控件 ...

  2. Android实现监听控件点击事件

    Android实现监听控件点击事件 引言 这篇文章主要想写一下Android实现监听点击事件的几种方法,Activity和Fragment实现起来有些方法上会有些不同,这里也略做介绍. 最近一直在忙一 ...

  3. 监听tableview的点击事件

    // 监听tablview的点击事件 - (void)addAGesutreRecognizerForYourView { UITapGestureRecognizer *tapGesture = [ ...

  4. highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度

    highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小  大] | ...

  5. Android 使用 OnTouchListener 接口监听双击或多击事件

    这里是使用 OnTouchListener 实现的监听双击 or 多击的监听器.通过 View.setOnTouchListener ,可以实现在任意 View 上监听双击事件. 网上有许多文章简单的 ...

  6. Android控件——监听按钮的点击事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAroAAAFTCAIAAABZPDiZAAAgAElEQVR4nOy9918UWfb///1jdu2uBs

  7. Android 设置软键盘搜索键以及监听搜索键点击事件

    如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮.调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch" ...

  8. cocos2d-x JS 弹出对话框触摸监听(吞噬点击事件遮挡层)

    在游戏中,我们经常会碰到一些弹窗,这些弹窗禁止点透,也就是禁止触摸事件传递到底层,我们称之为遮挡层,这些遮挡层,需要开发遮挡层,我们首先得了解cocos2d-js的触摸传递机制. 根据官方文档,我们可 ...

  9. Android 监听软键盘点击回车及换行事件

    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...

随机推荐

  1. MySQL 当记录不存在时插入(insert if not exists、dual )

    INSERT INTO clients(client_id, client_name, client_type)SELECT 10345, ’IBM’, ’advertising’FROM dualW ...

  2. excel 名次

    RANK.AVG 函数 全部显示 全部隐藏 返回一个数字在数字列表中的排位:数字的排位是其大小与列表中其他值的比值:如果多个值具有相同的排位,则将返回平均排位. 语法 RANK.AVG(number, ...

  3. qt creator源码全方面分析(2-4)

    目录 Qt Creator API Reference 库 核心库 其他库 插件 Qt Creator API Reference Qt Creator的核心基本上只是一个插件加载器.所有功能都在插件 ...

  4. PTA 汉诺塔的非递归实现(C 语言)

    借助堆栈以非递归(循环)方式求解汉诺塔的问题(n, a, b, c), 即将N个盘子从起始柱(标记为“a”)通过借助柱(标记为“b”)移动到目标柱(标记为“c”), 并保证每个移动符合汉诺塔问题的要求 ...

  5. C++ 与String有关的函数!!!

    String函数 1.字符串的输入 (1) string s; cin >> s;//碰到空格等分隔符会终端输入 /* string s; cin >> s;//如果输入 he ...

  6. 简单java web制作思路

    经过俩天的摸索,和学姐的帮助下终于做出来一个简单地网页版的学生信息添加的系统.接下来说一下答题的思路: 首先我个人习惯先做网页界面,创建3个jsp文件分别是添加界面,成功界面,失败界面.这件看起来更加 ...

  7. 后台实战——用户登录之JWT

    https://blog.csdn.net/jackcheng_ht/article/details/52670211

  8. 170.分组-group、permission、user的操作

    分组 1.Group.objects.create(group_name):创建分组. 2.group.permissions:某个分组上的权限.多对多关系. (1)group.permissions ...

  9. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  10. 构造并判断二叉搜索树-js

    class Node { constructor (val) { this.val = val this.left = this.right = undefined } } class Tree { ...