【发布时间】:2021-10-12 17:35:32
【问题描述】:
我已使用 FCM 和我的 Android 应用正确设置了通知中心。问题是当我的应用程序处于前台时,通知永远不会显示,但调试器会捕获 OnPushNotificationReceived,所以我知道设置正在运行。此外,当应用程序在后台运行或未运行时,会弹出通知。我认为这与我得到的代码有关: https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm
这是我的代码:
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID);
notificationBuilder.SetContentTitle(message.Title)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(message.Body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(context);
notificationManager.Notify(0, notificationBuilder.Build());
}
任何帮助将不胜感激
【问题讨论】:
-
我用你上面的代码测试了它,它可以成功运行。你能提供一个可以重现问题的例子吗?
-
如果不复制整个设置就很难重现。您能否确认当您的应用处于前台时您会看到通知?
-
是的,我可以看到通知到达,并且状态栏上会显示小图标。
标签: xamarin.android firebase-cloud-messaging azure-notificationhub