我在处理某些设备上的通知有效负载时出现问题。我通过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