Ios 当应用程序关闭时接收推送
我正在尝试保存推送通知负载附带的数据。它在应用程序运行时工作正常,但在应用程序关闭时工作不正常 当应用程序完全关闭且不在后台时,如何将推送通知中的数据保存到sqlite db 我需要在应用程序关闭并接收推送通知时执行此代码Ios 当应用程序关闭时接收推送,ios,objective-c,push-notification,Ios,Objective C,Push Notification,我正在尝试保存推送通知负载附带的数据。它在应用程序运行时工作正常,但在应用程序关闭时工作不正常 当应用程序完全关闭且不在后台时,如何将推送通知中的数据保存到sqlite db 我需要在应用程序关闭并接收推送通知时执行此代码 - (void) application:(UIApplication *)application didReceiveRemoteNotification:NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UI
- (void) application:(UIApplication *)application didReceiveRemoteNotification:NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//save data
NSLog(@" Received remote notifcation: %@", userInfo);
for (NSString *key in [userInfo allKeys])
{
NSString *data = [userInfo objectForKey:key];
NSLog(@"inside did register for notification .... %@ ---- > %@",key,data);
}
query = [NSString stringWithFormat:@"INSERT INTO accounts(email_address) VALUES ('%@')",data;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(mydb, [query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(compiledStatement))
{
NSLog( @"Error while inserting data: '%s'", sqlite3_errmsg(mydb));
}
else {
NSLog(@"New data inserted");
isneed=@"yes";
}
sqlite3_reset(compiledStatement);
}
else
{
NSLog( @"Error while inserting '%s'", sqlite3_errmsg(mydb));
}
}
以下内容摘自苹果公司的文档 使用此方法处理应用程序的传入远程通知。与application:DidReceiveMemoteNotify:method不同,该方法仅在应用程序在前台运行时调用,而系统在应用程序在前台或后台运行时调用此方法。此外,如果启用了远程通知后台模式,则系统会启动应用程序(或将其从挂起状态唤醒),并在推送通知到达时将其置于后台状态但是,如果用户强制退出应用程序,系统不会自动启动应用程序。在这种情况下,用户必须重新启动应用程序或重新启动设备,然后系统才会尝试再次自动启动应用程序
注意突出显示的文本,如果应用程序完全关闭,则不会启动该应用程序我没有将它们保存到CoreData中,但我将它们保存到NSUserDefaults中。这个解决方案是由苹果团队支持部推荐的,它只适用于iOS 10,我实现了它,它工作得很好!我离开链接: 程序如下:
我希望每一步都清楚。我将写一篇文章,用我的页面中的图像实现这个解决方案。你应该考虑的另一点是,它不适用于静默推送通知。你能显示你正在使用的代码吗?-(空)应用程序(UIApple *)应用DeDealEngReMyTooTrime:(NS词典)UsFieleFucFraceTeNo.HealtLe:(空格(^)(UIBACKEDFETCHETCHESREST))CuleTythHANDLER {//保存数据NSLO(@)“接收到的远程通知:%@”,userInfo);对于(NSString*输入[userInfo allKeys]){NSString*数据=[userInfo objectForKey:key];NSLog(@“内部未注册通知…”%@-->%@),输入,数据);}…你能展示你在问题中使用的格式化代码吗?这里是苹果协议参考的链接:不确定这是否会有很大帮助,抱歉没有任何代码可以提供
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSMutableArray *notifications = [NSMutableArray new];
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"];
notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];
if (notifications != nil){
[notifications addObject:self.bestAttemptContent.userInfo];
}else{
notifications = [NSMutableArray new];
[notifications addObject:self.bestAttemptContent.userInfo];
}
[defaultsGroup setObject:notifications forKey:@"notifications"];
}
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"];
NSMutableArray *notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];