解析iOS Swift推送通知历史记录

时间:2015-05-27 14:10:21

标签: ios xcode swift parse-platform notifications

我正在使用Swift推送通知(iOS 8)。问题是当应用关闭并且您收到多个通知时,它们将显示在通知警报中。当我触摸其中一个通知时,它将打开我的应用程序......但它将清除通知警报视图中的所有通知(不确定所谓的内容)。因此,我的所有推送通知都会丢失。我需要它们,因为它们具有我的应用程序所需的特定有效负载。

所以基本上,我需要的只是收到通知的数据(不仅仅是我打开的通知)。

我正在使用Parse推荐的代码。当app关闭并通过push打开时,将调用此函数。我使用constant let = notificationPayload来获取推送通知中的有效负载信息。但我只能从一次推送获得数据。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Enable storing and querying data from Local Datastore. 
    // Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
    Parse.enableLocalDatastore()

    // ****************************************************************************
    // Uncomment this line if you want to enable Crash Reporting
    // ParseCrashReporting.enable()
    //
    // Uncomment and fill in with your Parse credentials:
    Parse.setApplicationId("+++++", clientKey: "++++++++")


    //
    // If you are using Facebook, uncomment and add your FacebookAppID to your bundle's plist as
    // described here: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/
    // Uncomment the line inside ParseStartProject-Bridging-Header and the following line here:
    // PFFacebookUtils.initializeFacebook()
    // ****************************************************************************

    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.

        // Extract the notification data.
           if let notificationPayload = launchOptions?       [UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
    // notificationPayload have payload of only one push notification.
  }

        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 userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
        application.registerForRemoteNotificationTypes(types)
    }

    return true
}

任何goot信息,网页,这个教程? Parse.com文档没用,因为显然人们在应用关闭时只收到一个通知。

2 个答案:

答案 0 :(得分:0)

如果推送通知包含重要的有效负载,则可能必须将该有效负载保存在其他位置(例如,解析云数据)。

这样,即使iOS丢弃您的推送通知(或者如果用户决定丢弃它们),您的有效负载也将始终可用。

当应用程序启动时 - 在数据库中查找该有效负载并采取相应措施。

答案 1 :(得分:0)

我发现导致我的通知消失的原因。我有这个代码可以删除通知:

    UIApplication.sharedApplication().applicationIconBadgeNumber = 1
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    UIApplication.sharedApplication().cancelAllLocalNotifications()