好的我已经想通了。我现在有我的原始推送通知在iOS 9上工作,使用Xcode 8和Swift 2.3在iOS 10上工作。
我实现了这个通过进行以下修改我的AppDelegate:
1)在我的项目设置,功能选项卡下,我向下滚动到“推送通知”,并把它置于“ON”。这会自动生成包含关键“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中创建函数:
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”.. – vaibhav
你能请参考你指的是vaibhav的答案吗?我已经搜索了两天,无法找到适用于我描述的场景的任何问题或答案。 – Stanley
肯定[链接1](http:// stackoverflow。com/questions/39382852/didreceiveremotenotification-not-called-ios-10),[link 2](http://stackoverflow.com/questions/39267549/ios-how-to-integrate-push-notification-in-ios- 10).. – vaibhav