且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iOS 10上未收到推送通知,但在iOS 9及更早版本上工作

更新时间:2023-12-04 15:59:52

好的我有弄清楚了。我现在有了原始的Push Notifications,它正在iOS 9上运行,使用Xcode 8和Swift 2.3。

Ok I have figured it out. I now have my original Push Notifications that was working on iOS 9 working on iOS 10 with Xcode 8 and Swift 2.3.

我通过对AppDelegate进行以下更改来实现这一点:

I implemented this by making the following changes to my AppDelegate:

1)在我的项目设置中,在Capabilities选项卡下,我向下滚动到推送通知并将其转为ON。这会自动生成一个权利文件,其中包含密钥APS Environment和值development。

1) On my project settings, under the Capabilities Tab, I scroll down to "Push Notifications" and turn it to "ON". This automatically generates an entitlements file that contains the key "APS Environment" and with value "development".

2)在AppDelegate.swift中,我进行了几次代码更改。我首先导入UserNotifications框架:

2) In AppDelegate.swift I make several code changes. I start by importing the UserNotifications framework:

import UserNotifications

然后我让AppDelegate实现UNUserNotificationCenterDelegate协议:

Then I have AppDelegate implement the UNUserNotificationCenterDelegate protocol:

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中调用这个新创建的函数:

Then I call this newly created function inside didFinishLaunchingWithOptions:

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

我将didRegisterForRemoteNotificationsWithDeviceToken方法保持不变:

I leave my didRegisterForRemoteNotificationsWithDeviceToken method unchanged:

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实现两种新方法来处理推送通知的接收:

Now I implement two new methods for iOS 10 to handle the receiving of Push Notifications:

@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中为推送通知实现的任何方法。

I did not remove any of the methods I have previously implemented for Push Notifications in iOS 9.