swift - 在 Swift 2 iOS 9 中解析推送通知

标签 swift parse-platform apple-push-notifications swift2 ios9

我正在尝试使用 Parse.com 网站在我的第一个应用程序中显示推送通知,并且我遵循了教程和其他网站指南,当一切似乎都很好时,通知没有出现在我的设备中。

Parse.com 说我的证书没问题,当我按下“测试推送通知”时,它说一切都很好,但是通知没有出现在我的设备上。

它显示“已发送 0 个推送”。

这是我的代码 (AppDelegate):我必须进行一些更改,因为我使用的是 Swift 2,并且 Parse 指南中关于代码的一些地方有问题。

你知道为什么不起作用吗?我必须更改什么才能使其正常工作?

import UIKit

import Parse

import Bolts

extension UIColor {
convenience init(hex: Int) {
    let r = hex / 0x10000
    let g = (hex - r*0x10000) / 0x100
    let b = hex - r*0x10000 - g*0x100
    self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: 1)
}
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

    // Registramos la Push Notification
    if application.applicationState != UIApplicationState.Background {

        let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
        let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
        var pushPayload = false
        if let options = launchOptions {
            pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
        }
        if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
            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.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
        //application.registerForRemoteNotificationTypes(types)
    }

    Parse.setApplicationId("ynwxRlK1AYrB3ib36ELpyDbWEedg9LTWarTSFI4o", clientKey: "J7v458RE9yIgmvQk5UH3IRMLsozEnoIvjWFj3t6b")

    // Change navigation bar appearance
    UINavigationBar.appearance().barTintColor = UIColor(hex: 0x00B7BB)

    UINavigationBar.appearance().tintColor = UIColor.whiteColor()

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

    if let barFont = UIFont(name: "Avenir Next", size: 20.0) {
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:barFont]
    }

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

    return true
}

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

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

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

+info:当我在我的设备中查看通知时,它会显示来 self 的应用程序的信息并且一切都已激活。

最佳答案

相信您正在使用启用推送通知的配置文件,我建议您检查您的端口号 5223。这是发送推送通知的端口。我遇到了类似的问题,结果发现该端口由于防火墙限制而被阻止。

如果您不是这种情况,我建议您完成此故障排除 document 1document 2一步一步。

祝你好运!

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

相关文章:

ios - 搜索阿拉伯语文本,同时忽略变音符号或重音符号

android - 从解析中检索图像文件 - android

objective-c - Parse 项目中删除的桥接 header

ios - 我们可以使用 Twilio iOS SDK 进行 VOIP 推送通知吗?

iphone - 如果应用程序在推送通知上处于事件状态

ios - 如何在后台获取推送通知而无需在iOS的通知中轻按?

iOS swift : How to create object like UIAlertView that seemingly nils itself out after dismissal

ios - 调整自定义导航后退按钮的位置

java - 在 Android Studio 中设置 Parse 时出错

ios - 尝试在 UIView 上圆角时出现奇怪的行为