Swift 2 Parse 推送通知
Posted
技术标签:
【中文标题】Swift 2 Parse 推送通知【英文标题】:Swift 2 Parse push notification 【发布时间】:2015-09-20 16:05:32 【问题描述】:我正在尝试在 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)
【问题讨论】:
“它连接”是什么意思?您是否设置了推送证书(调试和推送)并将其上传到 Parse? 是的,我想是的.. 我的意思是它打印这个 print("ParseStarterProject 成功订阅了广播频道上的推送通知。"); 在解析时是否显示它发送了推送/是否收到? 现在我什么也没看到:\ 【参考方案1】:我遇到了同样的问题,可能是由以下原因之一引起的:
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)
【讨论】:
做了所有这些但没有工作..你知道 swift 2 的代码吗? 问题只是它没有显示推送..我认为它连接得很好 创建用户是什么意思?【参考方案2】:我有同样的问题。我用谷歌搜索并进行了实验,然后它开始完美运行。我认为代码更改没有任何区别。可以解析有问题吗?以下是我的代码(来自谷歌搜索),并且推送通知工作:
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)
【讨论】:
以上是关于Swift 2 Parse 推送通知的主要内容,如果未能解决你的问题,请参考以下文章
iOS Swift App 不会收到 Parse Server 发送的推送通知
Parse 和 Swift:didReceiveRemoteNotification 未收到带有“aps”的推送通知:“content-available”:1