解析推送通知在IOS 8.1上无法正常工作

时间:2014-11-17 08:12:41

标签: xcode parse-platform push-notification

这太奇怪了。 我有6个应用程序,所有这些应用程序共享相同的代码,现在使用我的iPhone 4s它完全可以正常工作。 由于我已经在iPhone 6 plus 8.1版本上下载了我的所有应用程序,因此我的6个应用程序中有4个无法使用推送通知(令人惊讶的是,它们都在应用程序委托中具有确切的代码)。

这是推送通知的代码:

- (BOOL)          application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     // xxx = for security reasons 
    [Parse setApplicationId:@"XXXXX" clientKey:@"XXXXX"];

    // more parse setting
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|
                                                     UIRemoteNotificationTypeAlert|
                                                     UIRemoteNotificationTypeSound)];

    if (application.applicationState != UIApplicationStateBackground) {
    // 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.
    BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)];
    BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
    BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
        [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    }

    //reset my badge of parse.com notification
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    if (currentInstallation.badge != 0) {
        currentInstallation.badge = 0;
        [currentInstallation saveEventually];
    }
    return YES;
}

- (void)                             application:(UIApplication *)application 
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];                
}

//parse setting
- (void)         application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateInactive) {
        // The application was just brought from the background to the foreground,
        // so we consider the app as having been "opened by a push notification."
        [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];

        //reset my badge of parse.com notification
        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        if (currentInstallation.badge != 0) {
            currentInstallation.badge = 0;
            [currentInstallation saveEventually];
        }
    }
}

// parse anylitics 
- (void)         application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo
      fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if (application.applicationState == UIApplicationStateInactive) {
         [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
    }
}

1 个答案:

答案 0 :(得分:0)

在iOS 8中,注册远程通知略有改变:

    if application.respondsToSelector("registerUserNotificationSettings:") {
        let userNotificationTypes = UIUserNotificationType.Alert | .Badge | .Sound
        let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types = UIRemoteNotificationType.Badge | .Alert | .Sound
        application.registerForRemoteNotificationTypes(types)
    }

在iOS 8上添加了新方法' registerUserNotificationSettings'尝试为本地和远程通知注册可能的通知类型。

P.S。 Swift中提供的示例,但很容易转换为Objective-C。