我正在尝试将所有推送的通知存储在 Core Data 中并将它们显示给用户。事实上,我已经实现了 didReceiveRemoteNotification:
,它的工作方式应该是这样的:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@", userInfo);
Notification *notify = [NSEntityDescription insertNewObjectForEntityForName:@"Notification" inManagedObjectContext:self.managedObjectContext];
notify.timeStamp = [NSDate date];
notify.alert = [[userInfo valueForKeyPath:@"aps"] valueForKeyPath:@"alert"];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Couldn't save to persistant store.");
}
}
但是当我将此代码放入 didFinishLaunchingWithOptions:
中的某些条件语句中时:
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
Notification *notify = [NSEntityDescription insertNewObjectForEntityForName:@"Notification" inManagedObjectContext:self.managedObjectContext];
notify.timeStamp = [NSDate date];
notify.alert = [[dictionary valueForKeyPath:@"aps"] valueForKeyPath:@"alert"];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Couldn't save to persistant store.");
}
}
}
我什么也没得到...我已经尝试创建 UIAlertView
并且它显示了来自 aps 的正确警报但是当我尝试保存到持久存储并在 MasterViewController 中显示它时没有任何反应。
也许有人知道为什么?是因为我打断了某种生命周期问题吗?
编辑
当保存成功时,我尝试将 UIAlertView
放入 didFinishLaunchingWithOptions:
中,它正在显示,所以我猜一切都已正确保存,但 NSFetchedResultController
不显示那些通知...为什么?
最佳答案
只有在应用程序处于运行状态时,您才能保存通知。如果应用程序在后台或关闭,推送接收但您无法访问和保存有效负载信息,因为在此期间不会执行任何委托(delegate)方法。您只能在服务器端的帮助下实现这一点,他们可以保存推送的通知。使用 api 调用传递此通知列表。
关于ios - 在本地存储远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622500/