安卓系统中通知点击时如何停止打开应用?

问题描述 投票:0回答:1

如何使通知像当我点击它通知删除,但没有行动。

   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText(""Message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setPriority(Notification.PRIORITY_MAX);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0, builder.build());

使用上面的代码应用程序打开通知点击。

这个通知来自FCM,我把它在自定义通知生成器。

android firebase-cloud-messaging android-notifications
1个回答
0
投票

如果我对你的问题理解正确的话,你想做一个通知,当用户点击它时,它就从通知抽屉中消失,什么都不做。

不如做一个PendingIntent的通知,什么都不做。

// Get Notification Manager
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Create Notification Channel if device OS >= Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT).let {
        notificationManager.createNotificationChannel(it)
    }
}

// Create PendingIntent with empty Intent
// So this pending intent does nothing
val pendingIntent = PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_ONE_SHOT)

// Configure NotificationBuilder
val builder = NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.drawable.ic_launcher_foreground)
    .setContentTitle("Title")
    .setContentText("Message")
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)

// Make the Notification
notificationManager.notify(0, builder.build())
© www.soinside.com 2019 - 2024. All rights reserved.