后续通知不会重新加载 Main Activity
subsequent notifications does not reload the Main Activity
我的应用程序创建了一个通知,当用户点击通知时,它会打开 Main Activity 和一些 EXTRA 数据。
当用户手动打开应用程序或第一次弹出通知时一切正常。
但问题是,当应用程序第一次被通知打开时,后续通知不会重新加载主Activity。如果应用程序被隐藏,它只会显示它(无需重新加载)。
这就是我创建通知的方式
fun createNotification(title: String, message: String, intent: Intent?) {
val mBuilder = NotificationCompat.Builder(
context,
NOTIFICATION_CHANNEL_ID
)
mBuilder.setSmallIcon(R.drawable.notification_ico)
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (intent != null) {
Log.e(TAG, "Intent dey")
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
mBuilder.setContentIntent(resultPendingIntent)
} else {
Log.e(TAG, "Unavailable dey")
}
val mNotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Enterprise Advantage",
importance
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern =
longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
mNotificationManager.createNotificationChannel(notificationChannel)
}
mNotificationManager.notify(0 /* Request Code */, mBuilder.build())
}
好的,根据@Rahul Shukla 的评论,
我对意图设置了一个操作
val intent = Intent(this, MainActivity::class.java)
intent.action = System.currentTimeMillis().toString()
然后将待处理意图的 Flag 更改为 PendingIntent.FLAG_ONE_SHOT
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT
)
我的应用程序创建了一个通知,当用户点击通知时,它会打开 Main Activity 和一些 EXTRA 数据。
当用户手动打开应用程序或第一次弹出通知时一切正常。
但问题是,当应用程序第一次被通知打开时,后续通知不会重新加载主Activity。如果应用程序被隐藏,它只会显示它(无需重新加载)。
这就是我创建通知的方式
fun createNotification(title: String, message: String, intent: Intent?) {
val mBuilder = NotificationCompat.Builder(
context,
NOTIFICATION_CHANNEL_ID
)
mBuilder.setSmallIcon(R.drawable.notification_ico)
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (intent != null) {
Log.e(TAG, "Intent dey")
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
mBuilder.setContentIntent(resultPendingIntent)
} else {
Log.e(TAG, "Unavailable dey")
}
val mNotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Enterprise Advantage",
importance
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern =
longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
mNotificationManager.createNotificationChannel(notificationChannel)
}
mNotificationManager.notify(0 /* Request Code */, mBuilder.build())
}
好的,根据@Rahul Shukla 的评论,
我对意图设置了一个操作
val intent = Intent(this, MainActivity::class.java)
intent.action = System.currentTimeMillis().toString()
然后将待处理意图的 Flag 更改为 PendingIntent.FLAG_ONE_SHOT
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT
)