Android: 防止应用程序在后台时在屏幕上弹出数据通知
Android: Prevent data notifications from popping up on the screen when app is in the background
我正在通过 FCM
发送 data
有效载荷,并在 FirebaseMessagingService
的 onMessageReceived()
方法中处理它们。问题是,当应用程序在后台运行时,每次设备处理推送通知时似乎都会弹出。我希望它们保持沉默,类似于 Android 传送 notification
有效负载的方式。
如何确保通知不会产生这种弹出效果?我在 Android 9.
客户端生成通知的方式如下:
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, myChannelId)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
这是它的通知方式:
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
notificationBuilder.SetChannelId(myChannelId);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(myId, notificationBuilder.Build());
当应用程序被杀死/在后台...
当请求中只传递'data' payload时,通知会弹出,由用户在'onMessageReceived'
中处理
当request只传'notification' payload时,会生成不弹窗通知,由android处理 OS
当请求中同时传入'data'和'notification' payload时,将生成不弹出的通知,由android OS和OS处理数据将可用于处理额外的意图。
因此,在我们的例子中(不想将通知显示为弹出窗口)在请求参数中传递 'data' 和 'notification' 有效载荷。 (这里会显示title和body,在notification payload中传递的内容)
参考官方link:https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
通知将以弹出效果出现('Heads Up') view like shown in the screenshot if you set the notification channel priority to IMPORTANCE_HIGH
or above, so to stop that happening, you should ensure the channel priority is IMPORTANCE_DEFAULT
或更低。
如果您有其他通知 想要使用抬头视图,那么您应该为此通知创建一个单独的 NotificationChannel。请注意,通知渠道在 notification settings for your app 中显示为 'Categories',允许用户选择他们希望从您那里看到的通知类型,两个想要不同优先级的通知是一个很好的指标,表明它们应该是在不同的类别中
使用android lifecyclehandler 并检查应用程序是在前台还是后台,如果应用程序在前台则不显示通知lifecyclerhandler
我正在通过 FCM
发送 data
有效载荷,并在 FirebaseMessagingService
的 onMessageReceived()
方法中处理它们。问题是,当应用程序在后台运行时,每次设备处理推送通知时似乎都会弹出。我希望它们保持沉默,类似于 Android 传送 notification
有效负载的方式。
如何确保通知不会产生这种弹出效果?我在 Android 9.
客户端生成通知的方式如下:
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, myChannelId)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
这是它的通知方式:
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
notificationBuilder.SetChannelId(myChannelId);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(myId, notificationBuilder.Build());
当应用程序被杀死/在后台...
当请求中只传递'data' payload时,通知会弹出,由用户在'onMessageReceived'
中处理
当request只传'notification' payload时,会生成不弹窗通知,由android处理 OS
当请求中同时传入'data'和'notification' payload时,将生成不弹出的通知,由android OS和OS处理数据将可用于处理额外的意图。
因此,在我们的例子中(不想将通知显示为弹出窗口)在请求参数中传递 'data' 和 'notification' 有效载荷。 (这里会显示title和body,在notification payload中传递的内容)
参考官方link:https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
通知将以弹出效果出现('Heads Up') view like shown in the screenshot if you set the notification channel priority to IMPORTANCE_HIGH
or above, so to stop that happening, you should ensure the channel priority is IMPORTANCE_DEFAULT
或更低。
如果您有其他通知 想要使用抬头视图,那么您应该为此通知创建一个单独的 NotificationChannel。请注意,通知渠道在 notification settings for your app 中显示为 'Categories',允许用户选择他们希望从您那里看到的通知类型,两个想要不同优先级的通知是一个很好的指标,表明它们应该是在不同的类别中
使用android lifecyclehandler 并检查应用程序是在前台还是后台,如果应用程序在前台则不显示通知lifecyclerhandler