【问题标题】:Push Notification for iOS7 and iOS8 in SwiftSwift 中 iOS 7 和 iOS 8 的推送通知
【发布时间】:2015-04-21 18:35:07
【问题描述】:

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

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

这在 Swift 中是不可能的。

如何同时注册 iOS7 和 iOS8 的推送通知?

【问题讨论】:

    标签: ios objective-c swift


    【解决方案1】:
    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")
        }
    

    应用代理:

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        //iOS7 method
    }
    
    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        //iOS8 method
    }
    

    【讨论】:

    • this should be done with getobjc method 是什么意思?
    • 我读到这种方式更好。 if objc_getClass("isRegisteredForRemoteNotifications") != nil { 从未尝试过这种情况。并留了个便条,以便我可以尝试一下。目前的确实有效
    • 我觉得你也可以这样做:UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings")
    • 是的,几种方法,不确定哪个是最好的
    • 如何接收通知的回调?我如何知道用户是否接受?
    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多