【问题标题】:Parse push notifications on iOS 8 using Swift使用 Swift 在 iOS 8 上解析推送通知
【发布时间】:2015-01-17 14:58:48
【问题描述】:

我需要制作使用 parse 推送通知的应用程序。我正在使用这个https://parse.com/apps/quickstart#parse_push/ios/existing 和这个https://gist.github.com/sawapi/a7cee65e4ad95578044d

代码已正确编译,我得到了

抱歉,发送推送时出错

在第一个网站上。来自 parse.com 的第一个教程完美运行。我已添加 *.p12 文件进行解析,但证书是由我的邮件而不是 ios 开发人员邮件制作的。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Parse.setApplicationId(MY_APP_ID,
            clientKey: MY_APP_KEY)
        PFUser.enableAutomaticUser()

        if application.respondsToSelector("isRegisteredForRemoteNotifications")
        {
            // iOS 8 Notifications
            //application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
            var types: UIUserNotificationType = UIUserNotificationType.Badge |
                UIUserNotificationType.Alert |
                UIUserNotificationType.Sound

            var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )

            application.registerUserNotificationSettings( settings )
            application.registerForRemoteNotifications()

            return true
        }
        else
        {
            // iOS < 8 Notifications
            application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
        }

        var defaultACL = PFACL()
        // If you would like all objects to be private by default, remove this line.
        defaultACL.setPublicReadAccess(true)
        PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser: true)

        // Override point for customization after application launch.
        return true
    }
    func application(application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
        var currentInstallation: PFInstallation = PFInstallation.currentInstallation()
        currentInstallation.setDeviceTokenFromData(deviceToken)
        currentInstallation.save()

        println("got device id! \(deviceToken)")
    }
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFPush.handlePush(userInfo)
    }
}

【问题讨论】:

  • registerUserNotificationSettings not isRegisteredForRemoteNotifications in if application.respondsToSelector("isRegisteredForRemoteNotifications")
  • 但是如果您在网站上遇到错误...再次检查证书... Parse.com 上有问题...
  • 我会尝试为我的 iOS 开发邮件制作证书
  • 下载这个项目...设置您的密钥(检查真实设备)..并检查您是否收到通知parse.com/apps/quickstart#parse_push/ios/new
  • 所以我需要对代码进行任何更改还是只检查证书?我可以在哪里发送通知?

标签: ios swift parse-platform


【解决方案1】:

这是解决方案:

import UIKit
import Parse




@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {



        // REGISTER FOR PUSH NOTIFICATIONS
        let notifTypes:UIUserNotificationType  = [.alert, .badge, .sound]
        let settings = UIUserNotificationSettings(types: notifTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
        application.applicationIconBadgeNumber = 0




return true
}


// MARK: - DELEGATES FOR PUSH NOTIFICATIONS
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let installation = PFInstallation.current()
    installation?.setDeviceTokenFrom(deviceToken)
    installation?.saveInBackground(block: { (succ, error) in
        if error == nil {
            print("DEVICE TOKEN REGISTERED!")
        } else {
            print("\(error!.localizedDescription)")
        }
    })
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("\(userInfo)")

    // PFPush.handle(userInfo)
    if application.applicationState == .inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(inBackground: userInfo, block: nil)
    }
}





func applicationDidBecomeActive(_ application: UIApplication) {

        let installation = PFInstallation.current()
        print("BADGE: \(installation!.badge)")
        if installation?.badge != 0 {
            installation?.badge = 0
            installation?.saveInBackground(block: { (succ, error) in
                if error == nil {
                    print("Badge reset to 0")
                } else {
                    print("\(error!.localizedDescription)")
            }})
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多