应用关闭时接收推送 [英] Receive push when app is closed

查看:91
本文介绍了应用关闭时接收推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存推送通知有效负载随附的数据.在应用程序运行时效果很好,但在关闭应用程序时效果不佳.

I am trying to save a data that comes with push notification payload. It works well when the app is running but not when the app is closed.

当应用程序完全关闭而不是在后台时,如何将推送通知中的数据保存到sqlite db.

how can I save data from push notification to sqlite db when the app is completely closed and not in the background .

我需要在关闭应用程序并收到推送通知时执行此代码

i need to do this code while the application is closed and receives a push notification

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

推荐答案

以下内容摘自Apple文档...

The following is taken from the apple documentation...

使用此方法为您的应用处理传入的远程通知.与application:didReceiveRemoteNotification:方法不同,该方法仅在您的应用程序在前台运行时才调用,而在您的应用程序在前台或后台运行时,系统会调用此方法.此外,如果启用了远程通知后台模式,则系统将启动您的应用程序(或将其从挂起状态唤醒),并在推送通知到达时将其置于后台状态. 但是,如果用户强制退出,系统不会自动启动您的应用.在这种情况下,用户必须重新启动您的应用或重新启动设备,然后系统才能尝试再次自动启动您的应用

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

请注意突出显示的文本,如果该应用程序完全关闭,则不会启动

Note the highlighted text, the app will not be started if it is completely closed

这篇关于应用关闭时接收推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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