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

使用BroadCastReceiver

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

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

在清单文件中注册

  1. <receiver
  2. android:name=".NotificationClickReceiver">
  3. </receiver>

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

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

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

兼容Android 8及以上

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

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. PMP--1.1 PMBOK体系框架描述

    PMBOK:Project Management Body Of Knowledge(项目管理知识体系) <PMBOK>中收录被普遍认可的良好实践. 普遍认可:在大部分时候适用于大多数项目 ...

  2. 有关css编写文字动态下划线

    <div class="main_text">哈哈这就是我的小视频</div> 上面为html代码 接下来进行css的编写 .main_text{ posi ...

  3. P5016 龙虎斗

    链接:P5016 ------------------------------------ 作为2019年的模拟,还是有必要写一些的 --------------------------------- ...

  4. #AcWing系列课程Level-2笔记——4. 浮点数二分算法

    浮点数二分算法 编写浮点数二分,记住下面的思路,代码也就游刃有余了! 1.首先找到数组的中间值,mid=(left+right)>>1,区间[left, right]被划分成[left, ...

  5. mysql升级后出现Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.

    安装了mysql5.7,用group by 查询时抛出如下异常: Expression #3 of SELECT list is not in GROUP BY clause and contains ...

  6. Mybatis Dao接口与Xml文件不匹配的问题:Invalid bound statement (not found)

    <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> <resources> <resource> <directory&g ...

  7. PHP0009:PHP基础-mysql

    以管理员省份启动记事本 修改host文件 插入外部sql数据

  8. MySQL 8 模式匹配

    MySQL 提供了两种模式匹配: 标准 SQL 模式匹配 基于扩展正则的模式匹配 SQL 模式匹配使用 LIKE 命令进行模式匹配操作, _ 匹配任意单个字符.% 匹配任意多个字符,包含 0 个字符 ...

  9. WinForm WebBrowser 设置IE版本

    public enum IeVersion { IE7 = , IE8 = , IE9 = , IE10 = , IE11 = }; /// <summary> /// 修改注册表信息来兼 ...

  10. C# compare different Encoding pattern between UTF8 and UTF32 based on Md5

    using System; using System.Text; using System.IO; using System.Security.Cryptography; static void Ma ...