You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin中实现AzurePushNotificationPlugin前台隐藏通知(安卓/iOS)

Got it, let's walk through how to fix this— I’ve used the CrossGeeks AzurePushNotificationPlugin enough to know the ins and outs here. The goal is to suppress notification banners when your app is in the foreground, but still fire the OnNotificationReceived event (or your custom logic). Here's how to do it for both Android and iOS:

Android Implementation
  • First, you’ll need to override the plugin’s default Firebase messaging behavior. In your Android project, create or modify a class that extends FirebaseMessagingService (if the plugin generated one, you can replace it with your custom version).
  • Override the OnMessageReceived method. Inside this method, add a check to see if your app is currently in the foreground. You can use the ActivityManager to verify this, or track a boolean flag that updates when your main activity resumes/pauses.
  • If the app is in the foreground, skip building and displaying the system notification. Instead, pass the notification data up to your shared code (like Xamarin.Forms) to trigger the OnNotificationReceived event manually. Using MessagingCenter is a simple way to do this.
  • Example code snippet for your custom service:
public override void OnMessageReceived(RemoteMessage message)
{
    if (IsAppInForeground())
    {
        // Collect notification data to pass to shared code
        var notificationData = new Dictionary<string, string>();
        if (message.Data != null)
        {
            foreach (var pair in message.Data)
            {
                notificationData.Add(pair.Key, pair.Value);
            }
        }
        // Send data to your shared project to trigger the event
        MessagingCenter.Send<object, Dictionary<string, string>>(this, "ForegroundNotification", notificationData);
    }
    else
    {
        // Let the plugin handle background notifications normally
        base.OnMessageReceived(message);
    }
}

private bool IsAppInForeground()
{
    var activityManager = (ActivityManager)GetSystemService(Context.ActivityService);
    var runningTasks = activityManager.GetRunningTasks(1);
    if (runningTasks != null && runningTasks.Count > 0)
    {
        var topActivity = runningTasks[0].TopActivity;
        return topActivity.PackageName == PackageName;
    }
    return false;
}
  • In your shared code, subscribe to the MessagingCenter event and hook it into your existing OnNotificationReceived logic so the same code runs as if the plugin triggered it.
  • Don’t forget to update your AndroidManifest.xml to point to your custom FirebaseMessagingService instead of the plugin’s default one.
iOS Implementation
  • On iOS, the magic happens in the UNUserNotificationCenterDelegate methods. In your project’s AppDelegate, override the WillPresentNotification method— this is called when a notification arrives while the app is active.
  • By default, iOS will show the notification banner, but you can tell it not to by passing UNNotificationPresentationOptions.None to the completion handler.
  • At the same time, extract the notification data and manually trigger the plugin’s NotificationReceived event so your shared code picks it up.
  • Example code snippet:
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    // Pull data from the incoming notification
    var userInfo = notification.Request.Content.UserInfo;
    var notificationData = new Dictionary<string, string>();
    foreach (var key in userInfo.Keys)
    {
        if (userInfo[key] is NSString value)
        {
            notificationData.Add(key.ToString(), value.ToString());
        }
    }

    // Trigger the plugin's notification received event
    AzurePushNotificationManager.DefaultManager.NotificationReceived?.Invoke(this, new AzurePushNotificationEventArgs(notificationData));

    // Tell iOS not to show any notification UI
    completionHandler(UNNotificationPresentationOptions.None);
}
  • Make sure you set UNUserNotificationCenter.Current.Delegate = this; in your FinishedLaunching method if the plugin hasn’t already done this for you.
Quick Notes
  • Double-check that your shared code has subscribed to the plugin’s OnNotificationReceived event correctly— this ensures your logic runs when the foreground notification is detected.
  • Test both scenarios: foreground (no banner, event fires) and background (normal notification appears) to make sure everything works as expected.

内容的提问来源于stack exchange,提问作者Daniel0b1b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:44:07