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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM