【问题标题】:Push notification does not appear if app is in background on iOS 9如果应用程序在 iOS 9 上处于后台,则不会出现推送通知
【发布时间】:2015-10-22 09:42:57
【问题描述】:

我遇到了推送通知问题,它只影响我运行 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: 触发)。

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

【问题讨论】:

  • 澄清一下,成功的“发送”响应并不意味着设备收到了推送,它只是意味着消息格式正确、证书匹配等。不保证推送通知待发货
  • @Paulw11 你说得对,我编辑了我的问题只是为了避免混淆。无论如何,我已经测试了很多次,它的行为总是这样:推送通知是“发送”的,它只有在我打开应用程序时才会出现,这很奇怪。即使应用程序在后台,它也应该出现。它应该显示在通知中心。
  • @JobertSá 我也有同样的问题。你终于成功修复了吗?
  • @Thomi 是的,我刚刚发布了我的答案:stackoverflow.com/a/36898516/564532 也许它对你的情况也有帮助?

标签: ios push-notification ios9


【解决方案1】:

最后,我必须做两件事让它对我有用:

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

【讨论】:

  • 通知确实出现在设备的通知中心,但应用委托方法application(_:didReceiveRemoteNotification:) 不会被调用,直到用户在通知中心没有点击通知。我认为这是正常行为,您能否确认您有此结果?
  • @Thomi 这是预期的行为。如果您想在应用程序处于后台时执行某些操作,当收到通知时,您还需要实现application(_didReceiveRemoteNotification:fetchCompletionHandler:) 此外,您需要启用“远程通知”,正如我在答案中提到的那样。
  • 是的,并且通知需要将“content-available”属性设置为1。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    看来你没事。这是我的代码,我也在 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)];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 2018-08-04
      相关资源
      最近更新 更多