swift - 推送远程通知

标签 swift swift2

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

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

最佳答案

我使用了这个链接,我发现它最有帮助

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)
}

关于swift - 推送远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382293/

相关文章:

swift - 使用 Alamofire 和 SwiftyJSON 发布对象数组

swift - macOS:在 NSMenu NSMenuItem 之间添加分隔线?

ios - 找不到类型的初始值设定项

ios - SpriteKit : how to add/remove node and avoid having mutation exception while enumerating?

ios - NSCoreDataCoreSpotlightDelegate 索引现有数据

swift - 找出已设置手势的 View

ios - 如何将带有 Storyboard和 xib 的 swift 项目转换为自定义 sdk/框架

ios - 是否可以为 UILabel 中的一个字符设置不同的颜色?

swift - 为什么调用 removeAtIndex : on a string invert the result if called within a string literal?

ios - Swift 保留周期解释