android notification 监听,如何监听Notification的点击事件

项目中加了个推的推送,需要根据消息透传获取数据自己生成通知,然后点击同种跳转到某个指定页面,这应该是是个比较常见的需求。

正常的实现:

@Override

public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {

String msgStr = new String(gtTransmitMessage.getPayload());

Log.e(TAG, msgStr);

Intent intent = new Intent(context, LoginActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

stackBuilder.addParentStack(LoginActivity.class);

stackBuilder.addNextIntent(intent);

PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

int id = (int) (System.currentTimeMillis() / 1000);

builder.setSmallIcon(R.drawable.push_small);

builder.setContentTitle("My title");

builder.setContentText(msgStr);

builder.setContentIntent(resultPendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(id, builder.build());

}

我自己生成一个notification然后再将要跳转的页面通过pendingIntent设置到notification中,这样当我们点击该notification时就会执行pendingIntent了。

BUT如果我们仅要执行页面跳转,还要在点击该notification后进行其他操作怎么办呢?比如谈个吐司。此时我们就需要对notification的点击事件进行捕捉,不过很显然系统并未提供相应的API。

不过在PendingIntent中提供了一个方法getBroadcast( ),我们可以通过广播的方式进行处理:当用户点击通知时,发送一个广播,我们可以在这个广播接收者中做需要的操作,上源码:

@Override

public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {

String msgStr = new String(gtTransmitMessage.getPayload());

Log.e(TAG, msgStr);

int id = (int) (System.currentTimeMillis() / 1000);

Intent intent = new Intent(context, ToMainActivityBroadcastReceiver.class);

intent.putExtra("notificationId",id);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setSmallIcon(R.drawable.push_small);

builder.setContentTitle("My title");

builder.setContentText(msgStr);

builder.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(id, builder.build());

}

广播接收者:

/**

* 跳转到主页面的广播接收者

* Created by Think on 2017/10/19.

*/

public class ToMainActivityBroadcastReceiver extends BroadcastReceiver {

public static final String IS_TO_FIRST_FRAGMENT = "isToFirstFragment";

@Override

public void onReceive(Context context, Intent intent) {

//用这个方法实现点击notification后的事件 不知为何不能自动清掉已点击的notification 故自己手动清就ok了

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(intent.getIntExtra("notificationId", -1));

Toast.makeText(context, "测试数据", Toast.LENGTH_LONG).show();

Intent toMainActivityIntent = new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

toMainActivityIntent.putExtra(IS_TO_FIRST_FRAGMENT, true);

context.startActivity(toMainActivityIntent);

}

}

注意:用这个方法实现点击notification后的事件,不知为何不能自动清掉状态栏中已点击过的通知,不过没事我们只要知道notification的id就可以自己手动清掉了。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(notificationId));

至于个推的具体使用,参考官网文档就已经很详细了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值