【Android】监听自定义通知栏消息事件
16lz
2021-01-23
1. 为Notification添加点击事件(点击推送消息后发送广播)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); CharSequence tickerText = "test_title"; Notification notification = new Notification(R.drawable.notification, tickerText, System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; CharSequence contentTitle = "contentTitle"; CharSequence contentText = "contentText"; Intent notificationIntent = null; notificationIntent = new Intent("NmTest"); PendingIntent contentIntent = PendingIntent.getBroadcast(Context, action, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(Context, contentTitle, contentText, contentIntent); notificationManager.notify(notifiId, notification);
2. 注册广播接收器监听点击行为
registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "通知被点击!"); }}, new IntentFilter("NmTest"));
注:在发送消息时,一定要设置
PendingIntent
的Action
不同,否则会发生如下情况:
当发出多条 action 和category 相同的推送之后,随便点击一条通知,广播接收器里面收到的 intent 里面拿出来的数据都是后面发送的那条。
相似问题:http://blog.csdn.net/z1074971432/article/details/17532139
更多相关文章
- 使用Android消息机制实现点击开始计数和暂停计数
- android 监听 USB 拔插广播消息
- 疯狂android讲义---事件处理2
- Android中如何使用基于监听的事件处理(上)
- android点击事件的四种方式
- Android 同时监听多个Button事件
- android亮屏、暗屏、解锁、关闭系统对话的监听事件