Ios 本地存储远程通知
我试图将所有推送通知存储在核心数据中,并向用户显示它们。事实上,我已经实现了Ios 本地存储远程通知,ios,iphone,objective-c,apple-push-notifications,Ios,Iphone,Objective C,Apple Push Notifications,我试图将所有推送通知存储在核心数据中,并向用户显示它们。事实上,我已经实现了didReceiveMemotentification:,它的工作方式应该是: -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"%@", userInfo); Notification *notify = [NSEntityDes
didReceiveMemotentification:
,它的工作方式应该是:
-(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.");
}
}
但当我将此代码放入中时,我在某些条件语句中使用选项完成了启动:
:
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显示正确的aler,但当我尝试保存到persistant store并在MasterViewController中显示它时,什么都没有发生
也许有人知道为什么?是因为我打断的某种生命周期问题吗
编辑
当保存成功时,我尝试将
UIAlertView
放入didfishlaunchingwithoptions:
中,它正在显示,因此我猜所有内容都已正确保存,但NSFetchedResultController
没有显示这些通知。。。为什么?尝试使用通知副本而不是原始通知。数据可能被删除,而您有一个空引用。事实上,除了显示存储的数据外,保存没有问题。我的解决方案是缓存在NSFetchedResultsController
中,您只能在应用程序处于运行状态时保存通知。如果应用程序处于后台或关闭状态,push接收到有效负载信息,但您无法访问和保存有效负载信息,因为在此期间不会执行任何委托方法。。您只能在服务器端的帮助下执行此操作,并且他们可以保存推送的通知。使用api调用传递此通知列表