IOS:Call method when local notification is received in background

戏子无情 提交于 2019-12-11 10:36:09

问题


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 !!


回答1:


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.



来源:https://stackoverflow.com/questions/29669544/ioscall-method-when-local-notification-is-received-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!