Android 应用在前台时不显示 Azure 通知中心通知

Android App not showing Azure Notification Hub notification while in foreground

我使用 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());
    }

如有任何帮助,我们将不胜感激

我发现问题是因为不存在 NotificationChannel,所以我在构造函数中创建了它:

    public AzureListener()
    {
        var channel = new NotificationChannel(MainActivity.CHANNEL_ID, "General", NotificationImportance.Default);
        var notificationManager = NotificationManager.FromContext(Application.Context);
        notificationManager.CreateNotificationChannel(channel);
    }

我添加这个后它起作用了。