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

查看:19
本文介绍了从推送通知更新 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.

参考:参考堆栈链接 1

参考堆栈链接 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)application:(UIApplication)application 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天全站免登陆