0
在我的應用程序中,我從Parse.com發送推送通知。我創建並安裝了所有證書和應用程序功能。但是,如果應用程序處於後臺,我會在通知中心收到一條通知,告知我正在點按並打開我的應用程序,但看不到包含發送消息的任何警報。代碼如下:通過Parse推送通知:當應用程序在後臺時顯示提醒
import UIKit
import Parse
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("appid",
clientKey: "clientkey")
// 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 userNotificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types: UIRemoteNotificationType = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Alert, UIRemoteNotificationType.Sound]
application.registerForRemoteNotificationTypes(types)
}
if let notification = launchOptions as? [String : AnyObject] {
if let notificationDictionary = notification[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, 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]) {
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
PFPush.handlePush(userInfo)
}
func clearBadges() {
let installation = PFInstallation.currentInstallation()
installation.badge = 0
installation.saveInBackgroundWithBlock { (success, error) -> Void in
if success {
print("cleared badges")
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
else {
print("failed to clear badges")
}
}
}
func applicationDidBecomeActive(application: UIApplication) {
clearBadges()
}
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 active state; here you can undo many of the changes made on entering the background.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
對不起,但我寫這個swift代碼: let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as?的NSDictionary 如果((通知)=零!){ 打印( 「有通知」) self.application(應用程序,didReceiveRemoteNotification:通知爲[NSObject的:AnyObject]) } 但是,當我通過以下方式打開我的應用點擊推送通知我看不到提醒消息(只有當應用程序處於前臺時,我才能看到提醒消息) –
是否已達到'didReceiveRemoteNotification'方法?也許嘗試顯示你自己的UIAlertController而不是使用Parse的Push Handler – JoshR604
現在我顯示一個UIAlertController:'if application.applicationState == .Inactive || application.applicationState == .Background {} {alertController = UIAlertController(title:「Boiade」,message:「」,preferredStyle:.Alert) let defaultAction = UIAlertAction(title:「OK」,style:.Default,handler:nil ) alertController.addAction(defaultAction) self.window?.rootViewController?.presentViewController(alertController,animated:true,completion:nil)'。現在,我如何在AlertController的消息中寫入發送推送通知的消息? –