【发布时间】:2015-07-13 11:20:18
【问题描述】:
我在在某些设备上处理通知负载时遇到了问题。我正在通过 Parse Cloud 功能向我的用户发送推送通知。
我正在使用以下方法来捕获通知并存储其有效负载,以便用户可以在专用视图中查看所有收到的通知。在我的个人设备上,我总是收到通知并正确保存,在我朋友的设备上,虽然通知到达,但如果应用程序在后台,则不会保存有效负载,而如果应用程序在前台,有效负载会被保存。
这可能是设备本身的问题吗?或者可能与电话提供商有关(我有 h3g,他有 Vodafone)?
-(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也不会被调用。也许他正在强制退出应用程序? -
我用我手上的两部手机做了很多测试。通知永远不会出现,一旦我打开应用程序就无法保存。有时甚至在我的手机上,所以我不再认为这可能是手机问题..这是我的代码的问题,但我不知道它有什么问题。
标签: ios objective-c push-notification payload