android 通知栏点击事件及8.0适配

设置 通知栏

    private void setNotification(String filePath, String recordType) {
        notificationManager = (NotificationManager) mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);

        // ========= 重点1 ============
        NotificationChannel notificationChannel= null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel("channel_id","channel_name", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription("chanel_description");
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 200, 200});
            notificationManager.createNotificationChannel(notificationChannel);
        }

         builder = new NotificationCompat.Builder(mainActivity, mainActivity.getPackageName());
         remoteViews = new RemoteViews(mainActivity.getPackageName(), R.layout.notification_layout);
         notification = builder
                .setSmallIcon(R.mipmap.nl_icon)//通知的构建过程基本与默认相同
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(false)
                .setShowWhen(false)
                .setLocalOnly(true)
                .setPriority(Notification.PRIORITY_MAX)
                .setCustomContentView(remoteViews)
                .setVibrate(new long[]{100, 200, 200, 200})
                .build();
        notification.flags=Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;

        Intent intentOne = new Intent(mainActivity, MainActivity.class);
        PendingIntent pendingIntentOne = PendingIntent.getActivity(mainActivity, 0, intentOne, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent = new Intent("notification_clicked"); // ========= 重点2============
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mainActivity, PENDINGINTENT_REQUEST_CODE, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setTextViewText(R.id.nl_tv_filename,filePath);
        remoteViews.setTextViewText(R.id.nl_tv_type,recordType);
        remoteViews.setOnClickPendingIntent(R.id.nl_rl_parent, pendingIntent);
        

        notificationManager.notify(NOTIFICATION_ID, notification);
    }

注册动态广播

IntentFilter intent=new IntentFilter("notification_clicked"); // ========= 重点3============
nlBroadcastReceiver=new NLBroadcastReceiver();
mainActivity.registerReceiver(nlBroadcastReceiver,intent);

接收广播

   class NLBroadcastReceiver extends BroadcastReceiver{

       @Override
       public void onReceive(Context context, Intent intent) {
           LogTools.i(TAG,"onReceive=intent=action="+intent.getAction());
            if(intent.getAction().equals("notification_clicked")){ // ========= 重点4 ============
         
            }
       }
   }

取消通知栏

    private void cancelNotification(){
        notificationManager = (NotificationManager) mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);
    }

注销广播

 mainActivity.unregisterReceiver(nlBroadcastReceiver );

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值