如何在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
OnMessageReceivedmethod. Inside this method, add a check to see if your app is currently in the foreground. You can use theActivityManagerto 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
OnNotificationReceivedevent manually. UsingMessagingCenteris 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
MessagingCenterevent and hook it into your existingOnNotificationReceivedlogic so the same code runs as if the plugin triggered it. - Don’t forget to update your AndroidManifest.xml to point to your custom
FirebaseMessagingServiceinstead of the plugin’s default one.
iOS Implementation
- On iOS, the magic happens in the
UNUserNotificationCenterDelegatemethods. In your project’sAppDelegate, override theWillPresentNotificationmethod— 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.Noneto the completion handler. - At the same time, extract the notification data and manually trigger the plugin’s
NotificationReceivedevent 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 yourFinishedLaunchingmethod if the plugin hasn’t already done this for you.
Quick Notes
- Double-check that your shared code has subscribed to the plugin’s
OnNotificationReceivedevent 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
相关产品推荐
相关产品推荐

