如何监听Notification的点击事件

2017-10-19  本文已影响3677人  拙峰朽木

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

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

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

上一篇下一篇

猜你喜欢

热点阅读