【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

你可能感兴趣的:(【Android】实用技巧)