gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:55:10 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com