Android 15 通知实现

94 阅读1分钟

在高阶android系统下的应用,通知的实现非常重要,直接影响了整个系统通知系统的使用,需要引起足够的重视。

private void initPendingIntent() {
        final String NM_ID = "high";
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(NM_ID,"BAT",NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);

//        Intent other = new Intent(MainActivity.this,OtherActivity.class);
        Intent intentDouyin = new Intent(MainActivity.this,OtherActivity.class);
        ComponentName cmp1 = new ComponentName("com.ss.android.ugc.aweme","com.ss.android.ugc.aweme.splash.SplashActivity");
        intentDouyin.setComponent(cmp1);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentDouyin, PendingIntent.FLAG_IMMUTABLE);
        // 将PendingIntent对象传递给NotificationManager,以便在通知被点击时启动Activity
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NM_ID)
                .setContentTitle("My Notification")
                .setContentText("Click to open MyActivity")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent);

        notificationManager.notify(1000, builder.build());
    }