简体   繁体   中英

IOS:Call method when local notification is received in background

I know this question looks duplicate but i did not found the exact answer as per my requirement.Doing app related to medical events with Below steps followed

1) connected the app to smart watch.

2) schedule the local notifications i. e when user needs to take
medicine 

3) when the schedule time arrives local notification fires and
delegate method is calling (did receive local notification)

4) At the time of  firing local notifications i am sending
message(sms) to the user from the app  that he has to take certain
medicine

Everything is working fine when the app is in foreground and when the app reaches to background only local notifications are firing no message is received by the user.Because no method is calling when the app is in background.But my whole application use case mostly depends on sending sms.

Is there any solution for this ? Any help will be welcomed !! Thnxx !!

You're going to need to explicitly run your method in a UIBackgroundTask. See example below:

    UIApplication * application = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];

    // your method call here

    [application endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;

I'm doing something similar and this works for me. Let me know if thats not working for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM