• Android监听消息通知栏点击事件


    Android监听消息通知栏点击事件

    使用BroadCastReceiver

    1 新建一个NotificationClickReceiver 类,并且在清单文件中注册!!

    public class NotificationClickReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //todo 跳转之前要处理的逻辑
            Log.i("TAG", "userClick:我被点击啦!!! ");
            Intent newIntent = new Intent(context, Main2Activity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        }
    }
    

    在清单文件中注册

     <receiver
             android:name=".NotificationClickReceiver">
     </receiver>
    

    在你需要创建通知栏的地方

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
            builder1.setSmallIcon(R.drawable.ic_launcher); //设置图标
            builder1.setTicker("显示第二个通知");
            builder1.setContentTitle("通知"); //设置标题
            builder1.setContentText("点击查看详细内容"); //消息内容
            builder1.setWhen(System.currentTimeMillis()); //发送时间
            builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
            builder1.setAutoCancel(true);//打开程序后图标消失
            Intent intent =new Intent (MainActivity.this,NotificationClickReceiver.class);
            PendingIntent pendingIntent =PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
            builder1.setContentIntent(pendingIntent);
            Notification notification1 = builder1.build();
            notificationManager.notify(124, notification1); // 通过通知管理器发送通知
    

    如果需要携带什么参数就在这里的intent包裹即可,NotificationClickReceiver可以接收到发送过来的intent

    兼容Android 8及以上

    // 版本升级通知框
                    NotificationManager notificationManager = (NotificationManager) MapActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification.Builder builder1 = new Notification.Builder(MapActivity.this);
                    // 通知框兼容 android 8 及以上
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        NotificationChannel channel = new NotificationChannel("11212313131", "NotificationName", NotificationManager.IMPORTANCE_DEFAULT);
                        channel.enableLights(true);
                        channel.setShowBadge(true);
                        notificationManager.createNotificationChannel(channel);
                        builder1.setChannelId("123456");
                    }
                    builder1.setSmallIcon(R.mipmap.touxiang); //设置图标
                    builder1.setContentTitle("这是一个通知"); //设置标题
                    builder1.setContentText("这是消息内容"); //消息内容
                    builder1.setWhen(System.currentTimeMillis()); //发送时间
                    builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
                    builder1.setAutoCancel(true);//打开程序后图标消失
                    Intent intent = new Intent(Activity.this, NotificationClickReceiver.class);
                    intent.putExtra("url","www.baidu.com");
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity.this, 0, intent, 0);
                    builder1.setContentIntent(pendingIntent);
                    Notification notification1 = builder1.build();
                    notificationManager.notify(124, notification1); // 通过通知管理器发送通知
    
    public class NotificationClickReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String url = intent.getStringExtra("url");
            Uri uri = Uri.parse(url);
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(i);
        }
    }
    
  • 相关阅读:
    spring boot 报错 Failed to read HTTP message
    spring boot 之 Mybatis 配置
    Java生成xlsx格式的excel文件
    遍历FTP目录及下载
    Spring 报错
    git ssh key生成
    spring mvc原理
    LightOJ 1154
    Light OJ 1153
    入栈出栈的顺序问题
  • 原文地址:https://www.cnblogs.com/wjw1014/p/12052589.html
Copyright © 2020-2023  润新知