简体   繁体   English

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

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

I'm having an issue with push notifications and it affects only my devices running iOS 9:我遇到了推送通知问题,它只影响我运行 iOS 9 的设备:

  1. I have my app installed in a set of devices.我在一组设备中安装了我的应用程序。 I run it and register for push notifications with the following code:我运行它并使用以下代码注册推送通知:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}
  1. The app is put on background on all devices.该应用程序在所有设备上都处于后台。
  2. I send push notifications to all devices - and get the response that it succeeded.我向所有设备发送推送通知 - 并获得成功的响应。
  3. All iOS 8 devices display the push notification in the notification center.所有 iOS 8 设备都会在通知中心显示推送通知。
  4. NO iOS 9 device display the push notification in the notification center.没有 iOS 9 设备在通知中心显示推送通知。
  5. Open the app in any of the iOS 9 devices in some random moment.在某个随机时刻在任何 iOS 9 设备中打开该应用程序。 The notification is displayed (triggered by application:didReceiveRemoteNotification: ).显示通知(由application:didReceiveRemoteNotification:触发)。

Has anything changed on iOS 9? iOS 9 有什么变化吗? How can I make the push notifications display also on iOS 9, even if the app is not active?即使应用程序未处于活动状态,如何使推送通知也显示在 iOS 9 上? I don't want to use notifications to fetch any content in background, I just need to display a message.我不想使用通知在后台获取任何内容,我只需要显示一条消息。

In the end, I had to do two things that made it work for me:最后,我必须做两件事才能让它对我有用:

  • Enable " Remote Notifications " for Background Modes under the Application capabilities (in the target settings)在应用程序功能下为后台模式启用“远程通知”(在目标设置中)
  • Send the notifications with high priority (when they were sent with low priority, they were not being handled with the application on background)以高优先级发送通知(当它们以低优先级发送时,它们不会被后台应用程序处理)

It seems you are OK.看来你没事。 This is my code, i get notification in iOS 9.0 also.这是我的代码,我也在 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)];
    }
}

I think that the user has to tap the notification to bring the app to the foreground and then application(_:didReceiveRemoteNotification:) will be called.我认为用户必须点击通知才能将应用程序带到前台,然后application(_:didReceiveRemoteNotification:)将被调用。

See here: https://www.raywenderlich.com/123862/push-notifications-tutorial请参阅此处: https : //www.raywenderlich.com/123862/push-notifications-tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM