美文网首页
如何监听Notification的点击事件

如何监听Notification的点击事件

作者: 拙峰朽木 | 来源:发表于2017-10-19 14:56 被阅读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));

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

相关文章

  • 如何监听Notification的点击事件

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

  • 事件

    题目 编写一个通用的事件监听函数 描述事件冒泡的流程 无限下拉的图片列表,如何监听每个图片的点击? 知识点 事件绑...

  • 监听事件之Target-Action、协议代理

    监听事件:事件指点击,触摸这样的手势所引发的消息,监听事件即监听这类消息。监听事件的目的是处理这类消息。 监听产生...

  • [Flutter]监听Android返回键事件

    需求监听Android返回键按钮点击事件 实现通过 WillPopScope 可以实现对返回键点击事件的监听,通过...

  • Java事件监听器的工作步骤介绍

    在程序开发中,经常需要对某些事件进行监听,如监听鼠标点击事件、监听键盘按下事件等,此时就需要使用事件监听器,事件监...

  • uniapp导航栏添加自定义按钮

    添加自定义按钮 注意:按钮的点击事件需要在页面监听onNavigationBarButtonTap事件 页面监听代...

  • 微信小程序

    微信小程序 - 监听 TabBar 切换点击事件 在小程序开发的时候想要监听系统的 TabBar 切换点击事件,只...

  • 手写通用的事件监听函数

    前言 在问题前,需要先理解事件冒泡、事件代理 可参考文章【事件代理】 动态数据列表里,如何监听每条数据的点击? 具...

  • 通过事件监听点击

    usingUnityEngine; usingUnityEngine.UI; usingUnityEngine.E...

  • 37 高级:MVC

    课堂笔记 如何监听 的提交事件 为什么不监听click 事件?因为仅监听'click'事件时,便无法监听用户使用回...

网友评论

      本文标题:如何监听Notification的点击事件

      本文链接:https://www.haomeiwen.com/subject/woiuuxtx.html