Swift 2 解析推送通知

标签 swift parse-platform push-notification swift2

我正在尝试在 Xcode 7.0 GM 中使用 Swift 2 进行解析推送通知。问题是它已连接但不显示推送..你知道为什么吗?预先感谢您

这是我的代码..当然我删除了我的 Parse ID,但在我的项目中有..我尝试使用 Parse 提供的代码,但我遇到了错误..所以我使用了 Github 上的一个项目,但它不是工作..

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

    Parse.setApplicationId("here there is my ID of course")

    PFUser.enableAutomaticUser()

    let defaultACL = PFACL();

    // If you would like all objects to be private by default, remove this line.
    defaultACL.setPublicReadAccess(true)

    PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser:true)

    if application.applicationState != UIApplicationState.Background {
        // Track an app open here if we launch with a push, unless
        // "content_available" was used to trigger a background push (introduced in iOS 7).
        // In that case, we skip tracking here to avoid double counting the app-open.

        let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
        let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
        var noPushPayload = false;
        if let options = launchOptions {
            noPushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil;
        }
        if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
            PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
        }
    }

    if application.respondsToSelector("registerUserNotificationSettings:") {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)



        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types: UIRemoteNotificationType = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Alert, UIRemoteNotificationType.Sound]
        application.registerForRemoteNotificationTypes(types)
    }


    return true



}



//--------------------------------------
// MARK: Push Notifications
//--------------------------------------

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()

    PFPush.subscribeToChannelInBackground("") { (succeeded, error) in
        if succeeded {
            print("ParseStarterProject successfully subscribed to push notifications on the broadcast channel.");
        } else {
            print("ParseStarterProject failed to subscribe to push notifications on the broadcast channel with error = %@.", error)
        }
    }
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    if error.code == 3010 {
        print("Push notifications are not supported in the iOS Simulator.")
    } else {
        print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
    }
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
}

最佳答案

我也遇到了同样的问题,可能是由以下原因之一引起的:

1) 确保您在真实设备上运行它,而不是模拟器。模拟器不支持推送通知。

2) 在您的设备上重新安装您的应用。这解决了我的问题。当它询问时,显然允许它发送推送通知。

2) 重新创建您的证书和配置文件。单击 here看看如何。

3) 确保您的证书有效并已安装。双击下载的配置文件进行安装。

此外,您可能想创建一个用户并尝试使用该用户,而不是自动用户。

编辑: 我建议您按照快速入门找到here 。我知道您会遇到错误,因此以下是修复这些错误的方法:

1) 删除let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Badge | UIUserNotificationType.Badge | UIUserNotificationType.Badge | UIUserNotificationType.Sound 并删除 let settings = UIUserNotificationSettings(forTypes: userNotificationTypes,categories: nil)。然后在删除行的位置输入 let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound],categories: nil)

2) 替换let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Alert UIRemoteNotificationType.Sound 行与 let types = UIRemoteNotificationType.Badge.union(UIRemoteNotificationType.Alert).union(UIRem‌​oteNotificationType.Sound)

关于Swift 2 解析推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681452/

相关文章:

ios - VC之间传递数据失败

ios - 如何快速在MKMapview上显示MKPinAnnotationView

java - 当应用程序未运行时,应用程序在解析推送通知时崩溃

swift - 准备 segue.. 从 UITableView 到 DetailView 解析对象

android - HMS+GMS 设备上的 HMS 推送通知问题

ios - Swift 字符串比较失败

xcode - 快速碰撞检测

parse-platform - react native _weakMap2 错误

android - WebSockets 能耗

ios - 推送插件 Cordova IOS GCM 未触发注册事件/未显示设备 token