我正在尝试使用Parse.com网站在我的第一个应用程序中显示推送通知,并且我已经遵循了教程和其他网站指南,当一切看起来都很好时,通知不会出现在我的设备中。
Parse.com说我的证书很好,当我按下“Test Push通知”时,它说一切都很好,但随后通知不会出现在我的设备中。
显示“0推送”。
这是我的代码(AppDelegate):我必须做一些更改,因为我使用的是Swift 2,而且在解析准则中有一些关于代码的错误。
你知道为什么不工作吗?我必须改变什么才能让它起作用?

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:.
}

}

+信息:当我在我的设备中查看通知时,它会显示来自我的应用程序的信息,并且所有内容都会被激活。

最佳答案

相信您正在使用推送通知启用配置文件,我建议您检查您的端口号5223。这是发送推送通知的端口。我也遇到过类似的问题,结果发现这个端口由于防火墙的限制而被阻塞了。
如果你不是这样的话,我建议你一步一步地进行故障排除。
祝你好运!

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

10-11 22:21
查看更多