iOS swift解析推送通知,推送发送0

时间:2015-08-31 22:10:23

标签: ios swift parse-platform notifications push

设备在安装类中正确注册(我猜),当我从解析网站发送推送通知时,状态为绿色,但是发送的推送是0,这是我的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.e

    Parse.enableLocalDatastore()

    // Initialize Parse.
    Parse.setApplicationId("xxxxxx",
        clientKey: "xxxxxx")


    // [Optional] Track statistics around application opens.
    PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)

    PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)

    var defaultACL = PFACL()
    defaultACL.setPublicWriteAccess(true)
    defaultACL.setPublicReadAccess(true)
    PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser: true)


    let userNotificationTypes = (UIUserNotificationType.Alert |  UIUserNotificationType.Badge |  UIUserNotificationType.Sound);

    let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    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 oldPushHandlerOnly = !self.respondsToSelector(Selector("application:didReceiveRemoteNotification:fetchCompletionHandler:"))
        let noPushPayload: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]
        if oldPushHandlerOnly || noPushPayload != nil {
            PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
        }
    }

    return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if application.applicationState == .Inactive  {
        // The application was just brought from the background to the foreground,
        // so we consider the app as having been "opened by a push notification."
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
}
func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    if application.applicationState == .Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Store the deviceToken in the current Installation and save it to Parse
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()
}

用户注册时:

 let installation = PFInstallation.currentInstallation()
                installation["user"] = PFUser.currentUser()
                installation["username"] = self.userName.text.lowercaseString
                installation.saveInBackground()

发送推送:

let pushQuery = PFInstallation.query()
        pushQuery!.whereKey("username", equalTo:"\(self.childName)") // Set channel

        // Send push notification to query
        let push = PFPush()
        push.setQuery(pushQuery) // Set our Installation query
        push.setMessage("\(self.parentName) adopted you, you earned $\(self.configureText(10 * childValue / 100)) !")

        push.sendPushInBackground()

1 个答案:

答案 0 :(得分:1)

您是否可以通过Parse网站发送推送通知?如果没有,那么你缺少一些证书。

假设self.childname包含正确的收件人用户名,这一切看起来都很好。你确定要启用客户端推送吗?

默认情况下,您无法直接从移动应用发送推送,因此您必须启用此功能才能使代码正常工作。 Parse iOS Push Notifications Tutorial