iOS 9中推送通知无法工作

13

我已经将我的设备升级到iOS 9,将我的Xcode环境升级到7.0 beta版本。在iOS 9中推送通知不起作用?

这是我的代码:

  float ver = [[[UIDevice currentDevice] systemVersion] floatValue];

    if(ver >= 8 && ver<9)
    {
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication] registerForRemoteNotifications];
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        }
    }else if (ver >=9){

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];


    }
    else{
        //iOS6 and iOS7 specific code
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];
    }

使用Xcode 6.4构建的iOS 8到8.3版本上,推送通知运行良好。


我遇到了同样的问题。如果你找到了解决方法,请告诉我。 - user2981810
有人找到了解决方法吗?我遇到了同样的问题。 - James Laurenstin
我也遇到了同样的问题。 - Hari Babu
我遇到了同样的问题,推送通知之前一直能用,甚至在iOS 9和xcode 7上也可以使用。但是上周我升级到了El Capitan、XCode 7.1和iOS 9.1之后,它们就停止工作了。我只测试了我的开发环境,但会测试其他环境。如果有人有任何想法,请尽快提供解决方案。我已经尝试使用Ray的教程重新生成证书,这通常都有效,但这次不行。 - crojassoto
使用Swift 2为iOS 9注册通知 - Scar
显示剩余2条评论
5个回答

5
在将代码添加到您的项目并创建证书和Ad-Hoc配置文件后,只需从您的X-code中启用即可。

enter image description here

了解更多详情 - iOS 9 推送通知教程


这是在将项目从Swift 2升级到3后发生在我身上的事情。令人烦恼的是,-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken仍然会被调用,只是除非勾选此选项,否则您将不会收到通知。 - Chris Birch
对我来说起了作用。当我转移到XCode 8时,我还将项目从Swift 2.0升级到3.0。 - oztune
是的,对于Swift 3.0和X-Code 8也是一样的。 - Abhishek Mishra

-1

我遇到了同样的问题。我通过创建一个Ad-Hoc Profile来解决它。我注意到当我使用development profile时,推送不会发生。另一件我注意到的事情是Device Token在每次新安装时都会改变,这有点奇怪,因为我们必须为每个新实例更新服务器。

您可以阅读这篇文章以获得更好的理解: iOS 9 Push Notifications


我知道设备令牌在重新安装时会发生更改,这不是问题。我每次登录时都会发送设备令牌。 - Anil Kukadeja
你尝试使用过Adhoc配置文件吗?对我来说,开发和分发证书似乎都可以工作。 - Sreejith
1
是的,我已经尝试过了。正如我上面提到的,你知道通知在使用相同的配置文件时可以在iOS 8上工作,但在iOS 9上却不行。 - Anil Kukadeja

-2
如果您使用Diawi安装应用程序,则推送通知在IOS 9上不起作用。请通过Xcode安装应用程序。

@chandresh:不,我没有通过Diawi安装。 - Anil Kukadeja
我使用Diawi安装iOS9应用程序,推送通知正常工作。 - Igor

-2
请尝试使用这段代码,因为它适用于我的IOS 9应用程序。
在AppDelegate DidFinishLaunchWithOption中编写代码。
if([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:  [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
   #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |    UIUserNotificationTypeSound)]; #endif
}

确保服务器上的推送通知已经经过了以下项目的仔细检查。
  1. 使用原始文件中的证书和私钥生成PEM文件
  2. 启用Apple服务并正确指向沙盒/生产环境
  3. 在下面的函数中接收推送通知令牌,并且没有返回任何错误,将其发送到发送APNS推送通知到设备令牌的LAMP服务。
  4. 在服务器上检查苹果APNS响应代码与发送到令牌的推送。
  5. 在设置中启用推送通知,如果您在“|”符号代码中出现任何错误,则可能会看到声音、徽章选项未显示在苹果通用通知设置中

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
        {
            NSString *devicetokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]; 
    
            DLog(@"Did Register for Remote Notifications with Device Token DATA (%@) \n STRING token (%@)", deviceToken,devicetokenString);
    
            //如果再次收到相同的令牌,请不要采取任何操作,否则保存在NSUserdefaults或系统中,并发送到服务器以注册此设备以在此处指定的令牌上发送推送通知。
        }
    
    -(void)application:(UIApplication *)application    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
        {
            DLog(@"Did Fail to Register for Remote Notifications");
            DLog(@"%s:%@, %@",__PRETTY_FUNCTION__,error, error.localizedDescription);
        }
    
    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        {
            DLog(@"Did Receive for Remote Notifications with UserInfo:%@", userInfo);
        }
    
希望这个解释得很清楚,如果需要帮助,请发布您的更新代码,这样我们可以帮助您。

-2
在didFinishLaunchingWithOptions方法中编写这段代码。
 UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];

并将此方法添加到AppDelegate中,就像我用于解析推送通知服务一样

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = @[ @"global" ];
[currentInstallation saveInBackground];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接