【Android】监听自定义通知栏消息事件

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"));

注:在发送消息时,一定要设置PendingIntentAction不同,否则会发生如下情况:
  当发出多条 action 和category 相同的推送之后,随便点击一条通知,广播接收器里面收到的 intent 里面拿出来的数据都是后面发送的那条。
相似问题:http://blog.csdn.net/z1074971432/article/details/17532139

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值