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

Xamarin iOS应用前台状态下Azure推送通知无法显示的问题求助

Hey there! Let's fix that issue where your Xamarin Forms iOS app isn't displaying push notifications when it's open. I've run into this exact scenario before, so here's what you need to check and implement:

Key Fixes for Foreground Push Notifications on iOS

iOS doesn't automatically show notification banners when your app is active in the foreground—you have to explicitly handle and display them yourself. Here's how to set this up:

1. Implement Foreground Notification Handling in AppDelegate

In your iOS project's AppDelegate.cs, update or add the DidReceiveRemoteNotification method to manually show notifications when the app is open:

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    // Extract notification content from the payload
    var aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
    if (aps == null)
    {
        completionHandler(UIBackgroundFetchResult.NoData);
        return;
    }

    var alertDict = aps.ObjectForKey(new NSString("alert")) as NSDictionary;
    var title = alertDict?.ObjectForKey(new NSString("title")) as NSString;
    var body = alertDict?.ObjectForKey(new NSString("body")) as NSString;

    // Show notification only if app is in foreground
    if (application.ApplicationState == UIApplicationState.Active && title != null && body != null)
    {
        var notificationContent = new UNMutableNotificationContent();
        notificationContent.Title = title.ToString();
        notificationContent.Body = body.ToString();
        notificationContent.Sound = UNNotificationSound.Default;

        // Create and schedule the notification request
        var request = UNNotificationRequest.FromIdentifier(
            Guid.NewGuid().ToString(), 
            notificationContent, 
            null);
        
        UNUserNotificationCenter.Current.AddNotificationRequest(request, error =>
        {
            if (error != null)
            {
                Console.WriteLine($"Failed to show foreground notification: {error.LocalizedDescription}");
            }
        });
    }

    completionHandler(UIBackgroundFetchResult.NewData);
}

2. Verify Your Push Payload Structure

Make sure the notification payload sent from Azure Notification Hub includes the alert field in the aps section—this is required for visible notifications. A valid payload should look like this:

{
    "aps": {
        "alert": {
            "title": "App Update Available",
            "body": "Check out our new features!"
        },
        "sound": "default"
    }
}

Avoid sending silent pushes (with only content-available: 1) if you want the notification to appear to the user.

3. Confirm Notification Permissions Include Alert Access

Double-check that you're requesting the Alert permission when setting up notifications in FinishedLaunching:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    // Request notification permissions
    var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
    UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
    {
        if (granted)
        {
            InvokeOnMainThread(() => app.RegisterForRemoteNotifications());
        }
    });

    // Rest of your FinishedLaunching code...
    return base.FinishedLaunching(app, options);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:26:15