从推送通知更新sqlite db,应用已关闭且未在后台iOS中运行 [英] update sqlite db from push notification, app is closed and not running in background iOS

查看:75
本文介绍了从推送通知更新sqlite db,应用已关闭且未在后台iOS中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在收到通知并且用户关闭/杀死应用程序时更新本地sqlite数据库.当应用程序处于后台或活动模式时,一切正常.

I am trying to update local sqlite database when notification received and app is closed/killed by user. Everything works fine when app is background or active mode.

参考资料: 参考堆栈链接2

这是我正在尝试的代码:

Here is code i am trying:

-(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);

       }  
   }

问题:为什么它不能在应用程序非活动状态下工作.有解决这个问题的方法吗?

Question : Why it is not working in application INACTIVE state. Is there a solution to this issue ?

- (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
{   
}
 }

推荐答案

-(void)应用程序:(UIApplication)应用程序didReceiveRemoteNotification:

-(void)application:(UIApplication )application didReceiveRemoteNotification:

仅当用户在后台运行应用程序时才点击通知横幅时,方法才被调用.如果您的应用被杀死,则需要处理

Method gets called only if user will tap on notification banner only if you app is in background. If you app is killed then you need to handle tap of banner in

didFinishLaunchWithOptions

didFinishLaunchWithOptions

方法.您可以使用以下代码来了解您是否点击了横幅,并且该应用被杀死了.

method.You can use the following code to know if you tap on banner and the app is killed.

// 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);       
}

这篇关于从推送通知更新sqlite db,应用已关闭且未在后台iOS中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆