【发布时间】:2019-04-19 09:10:59
【问题描述】:
当前,当用户在 Flutter 应用程序最小化时收到通知并点击通知时,它每次都会将用户重定向到 screen A。但我希望用户通过手动创建的深度链接登陆让我们说screen C。此外,如果用户从任何屏幕最小化应用程序,点击通知会将用户带到该屏幕。但无论用户从何处最小化应用程序或应用程序何时处于后台,点击通知应始终将用户重定向到screen C。我之前没有实现通知,所以这是我第一次处理这个问题,所以在这方面寻求帮助。通知类代码如下:
companion object {
const val CHAT_REQUEST_NOTIFICATION_ID = 1775
const val CHAT_CHANNEL_ID = "com.example.com.CHAT_CHANNEL_ID"
const val CHAT_CHANNEL_NAME = "Demo Notifications"
fun showChatNotification(context: Context, userName: String?, body: String?) {
createChatNotificationChannel(context);
val chatIntent = Intent();
val deeplink = generateDeepLink(userName);
chatIntent.setAction(Intent.ACTION_VIEW);
chatIntent.setData(Uri.parse(deeplink));
val chatPendingIntent = PendingIntent.getActivity(context, 100, chatIntent, PendingIntent.FLAG_ONE_SHOT)
val notification: Notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = Notification.Builder(context, CHAT_CHANNEL_ID)
.setContentIntent(chatPendingIntent)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_app_icon))
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(body)
.setOngoing(false)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.build()
} else {
notification = Notification.Builder(context)
.setContentIntent(chatPendingIntent)
.setSmallIcon(android.R.drawable.btn_star)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.app_icon))
.setAutoCancel(true)
.setVibrate(chatVibrationPattern)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentTitle("Message")
.setOngoing(false)
.setContentText(body)
.build()
}
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(CHAT_REQUEST_NOTIFICATION_ID, notification)
}
fun generateDeepLink(userId: String?): String {
return "https://demo.page.link/?link=https://demo.com/chat?user=$userId&apn=com.example.com&efr=1";
}
private fun createChatNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.deleteNotificationChannel(CHAT_CHANNEL_ID)
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(CHAT_CHANNEL_ID, CHAT_CHANNEL_NAME, importance)
notificationChannel.description = "Message"
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(notificationChannel)
}
}
}
我正在使用安卓模拟器 (6.0)。
【问题讨论】:
标签: android kotlin flutter android-notifications