Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

ios - Receive push when app is closed

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.

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));
    }
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following is taken from the apple documentation...

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...