didReceiveLocalNotification is never called in app icon touched launch - ios

I can't find any right solution even after long searching. What is normal scenario in iOS in belows?
When the app gets local notification,
1) didReceiveLocalNotification is invoked when the app is in foreground. and also is invoked when notification banned touched launch.
However, under the same situation,
2) didReceiveLocalNotification method is not invoked by app icon touched launch.
Stackoverflow or Apple document said
in case of 2), applicationWillEnterForeground -> didReceiveLocalNotification -> applicationDidBecomeActive delegates process is normal. but I've never seen didReceiveLocalNotification in the case of touching app icon.
Please, give me an advice case 2) is normal or not. Help me!
Edit -
After having couple of comments, I've found a link such as Handling local notifications when the user presses the icon instead of the alert
This approach would be working I believe. Thanks.
Edit2 -
Local Notification
This link would be helpful as well.

That method is only called when your app is launched as a result of interacting with a local notification. Not seeing it called during a normal app launch is expected behaviour.

Related

iOS when receive a remote notification can we know it and do something?

There is several situations when receiving a remote notification:
app is not launched at all
app is foreground
app is background
what I am looking for is that user haven't click the notification to launch or active the app. can we catch the receiving and do some thing in such three situation?
I know 2 is ok, how about 1 and 3, and how to do it? thx a lot.
somebody mentions widget or NotificationService? is it possible?
You can try adding a Notification Service Extension to your app.
iOS will launch your extension (NOT your app) in background when it receives a push notification. Obviously what you can do inside an extension is fairly limited.
There are two ways to be notified when your app moves to the background: implement the applicationWillResignActive() method in your app delegate, or register for the UIApplication.willResignActiveNotification notification anywhere in your app. This particular notification is sent as soon as your app loses focus, meaning that it's triggered when the user taps the home button once (to return to the home screen) or double taps the home button (to enter multi-tasking).

How to determine in applicationDidBecomeActive if triggered from a notification or springboard?

I have seen similar questions with answers stating to check the launchOptions, but i'm asking how to detect a user that has already launched the app previously. In other words, how do i detect an icon tap vs push inside func applicationDidBecomeActive(application: UIApplication)
There are a bunch of calls in the app delegate for notifications. If they don't get called, you were started some other way (not necessarily Springboard -- could be app-switcher, a deep-link URL -- perhaps other ways in the future).
Those should be called before applicationDidBecomeActive, but if not, you will have to delay until you find out if they are going to be called.

handleActionWithIdentifier when app isn't launched

I've been using interactive notification and it works well when my app is suspended in the background, but causes problems when my app has been terminated.
I've used NSLog to determine the app's lifecycle when the interactive notification is triggered when the app has not launched.
The following goes on, in the order listed, without the app visibly launching:
didfinishlaunching
handleActionWithIdentifier
viewdidload
viewwilllayoutsubviews
viewdidlayoutsubviews
viewdidappear
The app then seems to terminate without calling
didenterbackground or willTerminate
The reason why it's causing issues for me is because I create timers in viewdidload that are invalidated in didenterbackground.
Because didenterbackground isn't being called, when the app is subsequently launched after triggering an interactive notification I'm ending up with two instances of the timer.
Could anyone shed some light on why the app terminates but didenterbackground or willTerminate aren't being called?
You should not assume that a call to didfinishlaunching means that you are in the foreground. As #PaulW11 says in his comment, for local notifications when your app is not running, you get launched directly into the background, and never see a foreground-to-background transition, because you were never in the foreground in the first place.
The system will call applicationDidBecomeActive: if you are being launched into the foreground. You should put your code that starts your timers there.
The docs say that local notification notices get invoked in the background:
When the user taps a custom action in the alert for a remote or local
notification’s, the system calls the
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
or
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
method in the background so that your app can perform the associated
action.
You should write your application:handleActionWithIdentifier:forLocalNotification:completionHandler: method to check to see if it's being called from the foreground or from the background.
I use (Objective-C) code like this to tell if I'm in the foreground or the background:
BOOL inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;

Sliding to open on ios does not do the needed job

Sliding from the lock screen and/or pressing a notification from the notifications page... do both pass from applicationDidFinishLunchingWithOptions when the app is totally closed?
My concern is because, when the app is closed and not in background, whenever I press on the app from the notification, my app opens and goes where it has to go... but whenever I press slide to open, the app opens but it does not go to the page it should go.
The docs say that if the action button on the notification is pressed, it calls application:didFinishLaunchingWithOptions and passes in the notification payload. Later it says if the app is running in the foreground, it delivers the notification via application:didReceiveRemoteNotification:. This implies to me that when the app is backgrounded or not running, then application:didFinishLaunchingWithOptions is called. Otherwise, application:didReceiveRemoteNotification: is called.
application:didFinishLaunchingWithOptions: will only be called if your app is not launched already. While it is true that the options will include info on notifications if that is what ended up launching the app, what you want is to handle your local notification logic here:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

What delegate is called for a push notification if an application is in background mode

I read through almost a dozen questions on SO and Apple documentation, but still unclear on one case (mainly, because everybody use a little different terminology).
The case which I am interested in is:
a) An application is running in background state (as example VOIP).
I refer to background state as defined here
b) The application is registered for remote notifications.
c) Provider sends a push notification. iOS devices receives this notification.
What will happen next?
1) iOS will display according UI (alert, badge) and no delegate will be called until user clicks VIEW button on an alert or will tap application icon on Springboard.
My understanding this is what suppose to happen.
or
2) didReceiveRemoteNotification delegate will be called immediately
I would appreciate, if you can clear this out for me.
No delegate will be called until you eneter application by tapping on the notification.
Then this is the order of callbacks:
1) applicationWillEnterForeground
2) didReceiveRemoteNotification
2) applicationDidBecomeActive
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
will be called.

Resources