2015-07-13 30 views
1

我在处理某些设备上的通知有效负载时出现问题。我通过Parse Cloud功能向我的用户发送推送通知。推送通知有效负载未保存在某些设备上

我使用下面的方法捕获通知并存储其有效负载,以便用户可以在专用视图中查看所有收到的通知。在我的个人设备上,我通常会收到通知,并且它会正确保存在朋友的设备上,但通知是否到达,但是如果应用程序在后台,则不会保存有效内容,而如果应用程序处于前景,则会保存有效内容。

这可能是设备本身的问题吗?或者也许是与手机供应商有关的东西(我有h3g,他有沃达丰)?

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 
// Parse push handler will show a UIAlertView 
[PFPush handlePush:userInfo]; 

if (application.applicationState == UIApplicationStateInactive) { 

    // tha app is inactive, transitioning to or from the background   
    completionHandler(UIBackgroundFetchResultNoData); 

} else if (application.applicationState == UIApplicationStateBackground) { 

    // tha app is running in background 
    // add the notification to the notificationsArrayRecord 
    NSDate *now = [[NSDate alloc]init]; 
    NSDictionary *aps = userInfo[@"aps"]; 
    NSString *alertMessage = aps[@"alert"]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSMutableArray *notificationsArrayRecord = [[defaults arrayForKey:@"notificationsArrayRecord"] mutableCopy]; 
    [notificationsArrayRecord addObject:@[now,alertMessage]]; 
    [defaults setValue: notificationsArrayRecord forKey:@"notificationsArrayRecord"]; 

    // update the notifications counter 
    NSInteger pushCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"pushCount"]; 
    pushCount ++; 
    [defaults setInteger: pushCount forKey:@"pushCount"]; 
    [defaults synchronize]; 

    completionHandler(UIBackgroundFetchResultNewData); 
} else {   
    // the app is running in foreground 

    // add the notification to the notificationsArrayRecord 
    NSDate *now = [[NSDate alloc]init]; 
    NSDictionary *aps = userInfo[@"aps"]; 
    NSString *alertMessage = aps[@"alert"]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSMutableArray *notificationsArrayRecord = [[defaults arrayForKey:@"notificationsArrayRecord"] mutableCopy]; 
    [notificationsArrayRecord addObject:@[now,alertMessage]]; 
    [defaults setValue: notificationsArrayRecord forKey:@"notificationsArrayRecord"]; 

    // update the notifications counter 
    NSInteger pushCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"pushCount"]; 
    pushCount ++; 
    [defaults setInteger: pushCount forKey:@"pushCount"]; 
    [defaults synchronize]; 

    completionHandler(UIBackgroundFetchResultNewData); 

    // refresh the menu buttons and the notification counter 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidReceiveNotificationWhileActive" object:nil]; 
    } 
    } 
+0

您如何知道通知到达?他们是否显示在通知中心,或者您确定“didReceiveRemoteNotification”被调用?如果该应用程序被用户强制退出,该应用程序将不会被唤醒,并且不会调用“didReceiveRemoteNotification”。也许他正在强制退出应用程序? –

+0

我用我手上的两部手机做了很多测试。通知永远不会失败,一旦我打开应用程序,它就无法保存。有时甚至在我的手机上,所以我不认为它可能是一个电话问题..这是我的代码的东西,但我不知道它有什么问题。 – Diego

回答

1

我想这个问题是你如何处理应用程序的状态UIApplicationStateInactive。在这种情况下,您不存储信息。您应该在这种情况下存储它,因为当您收到通知时,应用程序显然可能处于此状态。这也解释了为什么它有时会失败。 另请参阅this question,其中指出当设备收到通知时,该应用有时处于状态UIApplicationStateInactive

你应该重构你的代码将数据存储在所有情况下:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 
    // Parse push handler will show a UIAlertView 
    [PFPush handlePush:userInfo]; 

    // add the notification to the notificationsArrayRecord  
    NSDate *now = [[NSDate alloc]init]; 
    NSDictionary *aps = userInfo[@"aps"]; 
    NSString *alertMessage = aps[@"alert"]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSMutableArray *notificationsArrayRecord = [[defaults arrayForKey:@"notificationsArrayRecord"] mutableCopy]; 
    [notificationsArrayRecord addObject:@[now,alertMessage]]; 
    [defaults setValue: notificationsArrayRecord forKey:@"notificationsArrayRecord"]; 

    // update the notifications counter 
    NSInteger pushCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"pushCount"]; 
    pushCount ++; 
    [defaults setInteger: pushCount forKey:@"pushCount"]; 
    [defaults synchronize]; 

    if (application.applicationState == UIApplicationStateInactive) { 
     // the app is inactive, transitioning to or from the background   
     completionHandler(UIBackgroundFetchResultNoData); 
    } else if (application.applicationState == UIApplicationStateBackground) { 
     // the app is running in background 
     completionHandler(UIBackgroundFetchResultNewData); 
    } else {   
     // the app is running in foreground 
     completionHandler(UIBackgroundFetchResultNewData); 

     // refresh the menu buttons and the notification counter 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidReceiveNotificationWhileActive" object:nil]; 
    } 
} 

更新: 我不知道有关调用completionHandler(UIBackgroundFetchResultNoData)applicationState(不知道这是好的),但也许您需要拨打completionHandler(UIBackgroundFetchResultNewData)来代替,在这种情况下,也需要存储数据。

此外,请确保您已正确配置所有内容,以便在后台接收通知,[请参阅此处]回答(https://stackoverflow.com/a/31450953/594074)。

+0

您的回答看起来对我非常有希望,我会立即尝试并在获得批准后接受它,并且我可以正确地尝试。同时谢谢你。 – Diego

+0

好的,我尝试了您的更正,问题仍然存在。有时候有效载荷会被保存,5分钟后它不会被保存。在两部手机上再次尝试。只有当应用程序处于后台状态时才会出现此问题,因为它在前台时总会成功保存有效内容。此外,如果我点击iOS通知徽章,一切都保存正确(在这种情况下,您的代码有效载荷得到保存两次,但这将是一个小问题)... – Diego

+0

...老实说,我放弃了这个事情..我担心这不是一个可靠的系统。我不知道这取决于我正在使用的同步方法还是我使用NSUserDefaults来保存有效内容的事实,事实是,有时应用程序会处于未调用didReceiveRemoteNotification方法的状态一些原因..你的回答对我仍然有用。谢谢。 – Diego

相关问题