我遇到问题在某些设备上处理通知有效负载。我正在通过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 :(得分: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)。