推送通知不在iOS 10上收到,但在iOS 9和之前的版本上

我有几个在Swift 2.2中编写的应用程序,使用Xcode 7.3进行编译,并且在App Store上运行。 这些应用程序利用推送通知,并在iOS 9.3及更早版本中正常工作。

但是,在已升级到iOS 10的设备上,我的应用程序不会收到任何推送通知。 仍在运行iOS 9的设备仍在接收通知。

认为这可能是一个证书或权利问题,我已经尝试了以下:升级我的应用程序Swift 2.3,添加了APS环境权利,并编译在Xcode 8,但这没有什么区别。

在我的AppDelegate我仍然有现有的方法注册推送通知,其中包括:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil) application.registerUserNotificationSettings(notificationSettings) 

即使在iOS 10上,这个注册似乎也是成功的,因为应用程序didRegisterForRemoteNotificationsWithDeviceToken然后被调用,所以我从APNS接收一个令牌。

问题是,当我发送一个推送通知到这个设备, 应用didReceiveRemoteNotification永远不会被调用。

现在, 这里说,UIApplicationDelegate上的方法已被弃用在iOS 10上,我应该实现userNotificationCenter( :didReceivewithCompletionHandler :)和userNotificationCenter( :willPresent: withCompletionHandler 🙂

问题是,我不是只针对iOS 10.我仍然需要应用程序在iOS 8和9上工作,所以我怀疑这是实现这些方法的正确方法。

如何获取现有应用程序的推送通知,以继续在已升级到iOS 10的设备上工作? 我是否需要重写代码? 我是否需要更新一些证书或授权,并在Xcode 8中用一些“新”设置重新编译?

好吧,我已经知道了。 我现在有我的原始推送通知,正在iOS 9上使用Xcode 8和Swift 2.3在iOS 10上工作。

我通过对我的AppDelegate进行以下更改来实现此目的:

1)在我的项目设置中,在function选项卡下,向下滚动到“推送通知”并将其打开到“打开”。 这将自动生成一个权利文件,其中包含“APS环境”关键字和值“开发”。

2)在AppDelegate.swift我做了几个代码更改。 我从导入UserNotifications框架开始:

 import UserNotifications 

然后我有AppDelegate实现UNUserNotificationCenterDelegate协议:

 class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate { 

然后我添加下面的方法:

 func registerForPushNotifications(application: UIApplication) { if #available(iOS 10.0, *){ UNUserNotificationCenter.currentNotificationCenter().delegate = self UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in if (granted) { UIApplication.sharedApplication().registerForRemoteNotifications() } else{ //Do stuff if unsuccessful... } }) } else{ //If user is not on iOS 10 use the old methods we've been using let notificationSettings = UIUserNotificationSettings( forTypes: [.Badge, .Sound, .Alert], categories: nil) application.registerUserNotificationSettings(notificationSettings) } } 

然后我在didFinishLaunchingWithOptions中调用这个新创build的函数:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { ... registerForPushNotifications(application) ... } 

我保持我的didRegisterForRemoteNotificationsWithDeviceToken方法不变:

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { //Implement this. Do something with the token I receive. Send it to my push notification server to register the device or something else I'd like to do with the token. } 

现在我实现了iOS 10的两个新方法来处理推送通知的接收:

 @available(iOS 10.0, *) func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { //Handle the notification } @available(iOS 10.0, *) func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { //Handle the notification } 

我没有删除我之前在iOS 9中推送通知的方法。

IOS 10有一个不同的通知工具包。

 import UserNotifications func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in if granted { UIApplication.shared.registerForRemoteNotifications() } } } else { let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound] let setting = UIUserNotificationSettings(types: type, categories: nil) UIApplication.shared.registerUserNotificationSettings(setting) UIApplication.shared.registerForRemoteNotifications() } } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){ let token = String(format: "%@", deviceToken as CVarArg) } 

我已经使用下面的步骤解决了这个问题:

步骤1:转到项目 – >目标 – >function – >推送通知 – >打开它

在这里输入图像说明 步骤2:解决与正确证书,configuration文件相关的问题

第3步:退出Xcode,清理并运行

在针对iOS 9和iOS 10推送通知问题的各种SO响应中,我已经尝试了以下testing:

1)更新通知呼叫以专门使用iOS 10设备的UNUserNotificationCenter

2)更新推送function,以便更新权利文件

我可以确认,做#2并保持所有现有的iOS 9推送代码相同将允许基本的推送通知通过iOS 10设备

当我的应用程序从IOS 9移到IOS 10时,Heroku上托pipe的Parse服务器发送的静默推送通知停止被应用程序接收。

我做的唯一让通知再次工作的变化是修改我的服务器上的Javascript,使内容可用的参数是一个整数1,而不是一个string“1”

  Parse.Push.send({ where: notifyDeletedRequestsQuery, data: { 'content-available': 1, refresh: constants.requestsClassName } }, { useMasterKey: true }); 

另请注意,内容可用不能与徽章,声音或警报一样出现在相同的推送通知中。