我在處理某些設備上的通知有效負載時出現問題。我通過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];
}
}
您如何知道通知到達?他們是否顯示在通知中心,或者您確定「didReceiveRemoteNotification」被調用?如果該應用程序被用戶強制退出,該應用程序將不會被喚醒,並且不會調用「didReceiveRemoteNotification」。也許他正在強制退出應用程序? –
我用我手上的兩部手機做了很多測試。通知永遠不會失敗,一旦我打開應用程序,它就無法保存。有時甚至在我的手機上,所以我不認爲它可能是一個電話問題..這是我的代碼的東西,但我不知道它有什麼問題。 – Diego