Swift 2解析推送通知
我正试图在Xcode 7.0 GM中使用带有Swift 2解析的推送通知。问题是它连接了,但没有显示推送。。你知道为什么吗?先谢谢你 这是我的密码。。当然,我删除了我的解析ID,但在我的项目中有..我试图使用Parse提供的代码,但出现了错误。。所以我在Github上使用了一个项目,但它不起作用Swift 2解析推送通知,swift,parse-platform,push-notification,swift2,Swift,Parse Platform,Push Notification,Swift2,我正试图在Xcode 7.0 GM中使用带有Swift 2解析的推送通知。问题是它连接了,但没有显示推送。。你知道为什么吗?先谢谢你 这是我的密码。。当然,我删除了我的解析ID,但在我的项目中有..我试图使用Parse提供的代码,但出现了错误。。所以我在Github上使用了一个项目,但它不起作用 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:
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) 重新创建证书和资源调配配置文件。单击以查看如何创建 3) 确保您的证书有效且已安装。双击下载的配置文件以安装它 另外,您可能希望创建一个用户并尝试使用它,而不是自动用户 编辑: 我建议您按照找到的快速入门指南进行操作。我知道您会遇到错误,下面是如何修复这些错误: 1) 删除
let userNotificationTypes=UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
并删除let settings=UIUserNotificationSettings(forTypes:userNotificationTypes,categories:nil)
。然后在删除行的位置键入let settings=ui用户通知设置(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)
我也有同样的问题。我在谷歌上搜索和试验,然后它就开始完美地工作了。我认为代码的改变并没有带来什么不同。可能是因为有问题吗?以下是我的代码(来自谷歌)和推送通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
Parse.setApplicationId("***your id ***",
clientKey: "*** your key ***")
// Register for Push Notitications
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 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, .Alert, .Sound]
application.registerForRemoteNotificationTypes(types)
}
*/
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
// check if app opened because user tapped on notification
if let launchOptions = launchOptions as? [String : AnyObject] {
if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notificationDictionary)
}
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
/*
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation["user"] = PFUser.currentUser()
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)
}
}
你说的“它连接”是什么意思?您是否设置了推送证书(包括调试和推送)并将其上载到Parse?是的,我想是的。。我的意思是打印这个打印(“ParseStarterProject成功订阅了广播频道上的推送通知”);在解析时,它是否显示它发送了推送/是否收到了推送?现在我什么也看不到:\n完成了所有这些,但没有工作。。你知道swift 2的代码吗?问题是它没有显示推力。。它连接良好我想你说的创建用户是什么意思?