推送远程通知

时间:2016-07-14 19:05:01

标签: swift swift2

我需要在我的应用程序中创建一个接收远程信息的代码,并在应用程序处于后台时将其推送给用户,我在网上阅读我需要在appDelegate上使用didReceiveRemoteNotification,以使用远程推送通知。我读了一些关于我需要密钥和证书的内容,我不明白如何使用didReceiveRemoteNotification

我需要学习如何推送远程通知以及如何使用。我想要一个教程或示例如何使用swift 2.3创建它。

1 个答案:

答案 0 :(得分:0)

我使用此链接,我发现它是最有帮助的

http://www.appcoda.com/push-notification-ios/

我用这个应用程序进行测试

https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12

这是我在AppDelegate中的代码

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

    registerForPushNotifications(application)

    print(UIDevice.currentDevice().identifierForVendor!.UUIDString)

    return true
}


func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    print("Device Token:", tokenString)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register:", error)
}