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

标签 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()

最佳答案

你能通过 Parse 网站发送推送通知吗?如果不是,那么您缺少一些证书。

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

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

关于iOS swift 解析推送通知,推送发送 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320718/

相关文章:

ios - xcodebuild存档失败:选择错误的SDK?

ios - 具有输入输出的 rxswift viewmodel

arrays - 表不会加载/显示 CloudKit 数据

android - 使用 Parse 运行简单的 android facebook 集成程序时出现异常

javascript - 更新 parse.com 中的用户对象

ios - NSMutableArray Init 的麻烦

ios - 与 archiveRootObject :toFile: and writeToFile: 的差异

c# - Unity3D iOS 64 位的 IL2CPP 编译器运行时错误

swift - 以编程方式增加 NSAlert 的字体大小

xcode - Swift Parse.com 无法检索对象