2015-07-13 32 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

相關問題