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

iOS平台Xamarin.Forms应用运行时无法接收UrbanAirship推送通知

问题分析:UrbanAirship前台推送接收失败

看起来你遇到的问题是iOS应用在前台运行时无法接收UrbanAirship推送,这其实是iOS的默认行为——当应用处于前台状态时,系统不会自动弹出通知横幅,需要我们手动实现对应的回调来处理这个场景。我帮你检查了代码,发现几个关键的遗漏点:

1. 缺少前台通知接收的回调方法

iOS 10及以上版本中,前台接收推送需要实现UNUserNotificationCenterDelegate的WillPresentNotification方法。这个方法会在应用前台收到推送时被触发,你需要在这里处理通知的展示逻辑,同时告知系统要展示通知的哪些内容(比如弹窗、声音、角标)。

2. 未设置UNUserNotificationCenter的代理

你需要在FinishedLaunching方法中将AppDelegate设置为UNUserNotificationCenter的代理,这样系统才会把前台推送的事件传递给你的代码。


修改后的AppDelegates.cs代码

public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions) 
{ 
    UAirship.TakeOff(); 
    Xamarin.Forms.Forms.Init(); 
    LoadApplication(new App()); 
    UIApplication.SharedApplication.StatusBarHidden = false; 
    UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false); 
    _networkManager = new NetworkManager(); 
    UAirship.Push.UserPushNotificationsEnabled = true; 
    string chanelid = UAirship.Push.ChannelID;

    // 新增:设置UNUserNotificationCenter的代理为当前AppDelegate
    UserNotifications.UNUserNotificationCenter.Current.Delegate = this;

    return base.FinishedLaunching(uiApplication, launchOptions); 
} 

// 新增:前台接收推送的回调方法
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UserNotifications.UNUserNotificationCenter center, UserNotifications.UNNotification notification, Action<UserNotifications.UNNotificationPresentationOptions> completionHandler)
{
    // 自定义前台推送的展示方式,比如弹出Alert
    var alert = new UIAlertView("前台推送通知", notification.Request.Content.Body, null, "关闭", "查看");
    alert.Show();
    
    // 告诉系统要展示通知的Alert和Sound
    completionHandler(UserNotifications.UNNotificationPresentationOptions.Alert | UserNotifications.UNNotificationPresentationOptions.Sound);
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")] 
public void DidReceiveNotificationResponse(UserNotifications.UNUserNotificationCenter center, UserNotifications.UNNotificationResponse response, Action completionHandler) 
{ 
    var alert = new UIAlertView("Title1", "Message1", null, "Cancel", "OK"); 
    alert.Show(); 
    //TAPPED NOTIFICATION 
    completionHandler(); // 不要忘记调用completionHandler,否则可能影响后续推送处理
} 

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) 
{ 
    var alert = new UIAlertView("Title2", "Message2", null, "Cancel", "OK"); 
    alert.Show(); 
    completionHandler(UIBackgroundFetchResult.NewData); // 同样需要调用completionHandler
} 

额外检查点

  • 确保你的项目中包含正确配置的AirshipConfig.plist文件,里面填写了正确的UrbanAirship App Key、Secret,并且根据环境设置了developmentAppKey/productionAppKey。
  • 你的info.plist已经正确添加了remote-notification后台模式,这部分配置是没问题的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:45:55