apple-push-notifications - 在後台iOS中,從推送通知更新SQLite資料庫,應用程序關閉且未運行

  显示原文与译文双语对照的内容
122 1

當收到通知和應用程序關閉/終止時,我正在嘗試更新本地SQLite資料庫。當應用程序的背景或者活動模式時一切都正常。

參考:參考堆棧鏈接 1

參考堆棧鏈接 2

以下是我嘗試的代碼:

-(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"App Background :%@",userInfo);
 if(application.applicationState == UIApplicationStateInactive) {
 NSLog(@"Inactive");
//Show the view with the content of the push
 [self BackgrounCall:userInfo];
 completionHandler(UIBackgroundFetchResultNewData);
 } else if (application.applicationState == UIApplicationStateBackground) {
 NSLog(@"Background");
//Refresh the local model
 [self BackgrounCall:userInfo];
 completionHandler(UIBackgroundFetchResultNewData);
 } else {
 NSLog(@"Active");
//Show an in-app banner
 [self BackgrounCall:userInfo];
 completionHandler(UIBackgroundFetchResultNewData);
 } 
 }

問題:為什麼它不能在應用程序非活動狀態下運行。這個問題有解決辦法?

- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
//Override point for customization after application launch.
[[Database shareDatabase] createEditableCopyOfDatabaseIfNeeded];
if(launchOptions!= nil)
{ 
//opened from a push notification when the app is closed
 NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
 if (userInfo!= nil)
 {
 NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
 [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
 }
}
else
{ 
}
 }
时间:原作者:0个回答

100 3

- ( 空心) 應用程序:( UIApplication ) 應用程序 didReceiveRemoteNotification:

只有當用戶在後台應用時才點擊通知橫幅,才會調用方法。如果你的應用被殺死,你需要處理

didFinishLaunchWithOptions

method.You 可以以使用下面的代碼知道你是否點擊了標題,並且應用程序已經被殺死。

//Push Notification Handling
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSDictionary *pushDic = [[NSDictionary alloc]init];
pushDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (pushDic)
{
 NSLog(@"got push when app killed->%@",pushDic); 
}
原作者:
...