簡體   English   中英

在Swift中為iOS7和iOS8推送通知

[英]Push Notification for iOS7 and iOS8 in Swift

我想在Swift中為iOS7和iOS8注冊推送通知。 回到Objective-C,我可以做類似的事情:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
        UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone;
#endif

在Swift中這是不可能的。

如何同時注冊iOS7和iOS8的推送通知?

static func setupPushNotifications() {
        // Register for Push Notitications
        let userNotificationTypes:UIUserNotificationType = [.Alert, .Sound, .Badge]

        if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications") { //this should be done with getobjc method
            // iOS 8 Notifications
            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            UIApplication.sharedApplication().registerUserNotificationSettings(settings)
            UIApplication.sharedApplication().registerForRemoteNotifications()
        } else {
            // iOS < 8 Notifications
            UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
        }
        print("Push notifications setup complete")
    }

的appdelegate:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    //iOS7 method
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    //iOS8 method
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM