如果应用程序在 iOS 9 上处于后台,则不会显示推送通知

Push notification does not appear if app is in background on iOS 9

我遇到推送通知问题,它只影响我的设备 运行ning iOS 9:

  1. 我在一组设备上安装了我的应用程序。我 运行 它并使用以下代码注册推送通知:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}
  1. 该应用程序在所有设备上都置于后台。
  2. 我向所有设备发送推送通知 - 并收到成功的响应。
  3. 所有iOS 8台设备在通知中心显示推送通知。
  4. 否iOS 9台设备在通知中心显示推送通知
  5. 随机在 iOS 9 台设备中的任意一台上打开应用程序。显示通知(由 application:didReceiveRemoteNotification: 触发)。

iOS9 有什么变化吗?如何在 iOS 9 上也显示推送通知,即使该应用未处于活动状态? 我不想使用通知在后台获取任何内容,我只需要显示一条消息。

看来你还好。这是我的代码,我也在 iOS 9.0 中收到通知。

-(void)registarPushNotification:(UIApplication*)application
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {    
        UIUserNotificationType types = UIUserNotificationTypeBadge |  UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

        [application registerUserNotificationSettings:mySettings];
        [application registerForRemoteNotifications]; 
    }
    else
    {
        [application registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }
}

我认为用户必须点击通知才能将应用程序带到前台,然后 application(_:didReceiveRemoteNotification:) 将被调用。

看这里:https://www.raywenderlich.com/123862/push-notifications-tutorial

最后,我不得不做两件事让它对我有用:

  • 在应用程序功能下(在目标设置中)为后台模式启用“远程通知
  • 发送高优先级的通知(当它们以低优先级发送时,后台应用程序不处理它们)