我正在尝试在Xcode 7.0 GM中使用Swift 2解析推送通知。问题是它连接了但没有显示推动。您知道为什么吗?先感谢您
这是我的代码..当然我删除了我的Parse ID,但是在我的项目中..我试图使用Parse提供的代码,但是我遇到了错误..所以我在Github上使用了一个项目,但是它不起作用。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("here there is my ID of course")
PFUser.enableAutomaticUser()
let defaultACL = PFACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true)
PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser:true)
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 preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var noPushPayload = false;
if let options = launchOptions {
noPushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil;
}
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
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 = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Alert, UIRemoteNotificationType.Sound]
application.registerForRemoteNotificationTypes(types)
}
return true
}
//--------------------------------------
// MARK: Push Notifications
//--------------------------------------
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
PFPush.subscribeToChannelInBackground("") { (succeeded, error) in
if succeeded {
print("ParseStarterProject successfully subscribed to push notifications on the broadcast channel.");
} else {
print("ParseStarterProject failed to subscribe to push notifications on the broadcast channel with error = %@.", error)
}
}
}
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)
}
}
最佳答案
我有同样的问题,可能是由以下原因之一引起的:
1)确保在真实设备而非模拟器上运行它。模拟器不支持推送通知。
2)在您的设备上重新安装您的应用程序。这解决了我的问题。当它询问时,显然允许它发送推送通知。
2)重新创建您的证书和配置文件。单击here查看操作方法。
3)确保您的证书有效并已安装。双击下载的配置文件进行安装。
另外,您可能想要创建一个用户并尝试使用该用户,而不是自动用户。
编辑:
我建议您遵循here中找到的快速入门。我知道您会收到错误,因此下面是解决方法:
1)删除let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
并删除let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
。然后在删除行的位置键入let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
。
2)用let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
替换let types = UIRemoteNotificationType.Badge.union(UIRemoteNotificationType.Alert).union(UIRemoteNotificationType.Sound)
行