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

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).

Related

Swift iOS system notification dismiss calback

Question:
Is there a way to detect if you get a system notification and you swipe away the notification banner (e.g. the alarm that triggers a system notification)?
More practical example:
In your app, you are doing some stuff and suddenly you get a notification from another app. Instead of tapping on the notification, you swipe up the notification (you dismiss it that way). After doing that, I want to capture that in the app.
Why:
Currently, I have a bug in the app, so if I can capture this action, I would be able to fix this bug.
Thanks in advance!
When notification appears, the willResignActiveNotification won't get called, therefore willEnterForegroundNotification won't get called, too.
The best solution here is to catch incoming notification and show the custom view describing this notification instead of system one. Thus, you can detect all actions/timings you need.

Handling notification in the background before user taps on them - Swift 3

In swift 3, how can I handle a local notification while the app is in the background and before the user taps on the notification?
UserNotificationController.userNotificationCenter(_:willPresent:withCompletionHandler:) is only called when the user taps on the notification.
My goal is to create a local notification that displays a screen similar to an incoming call in which the user can swipe to pick up or hang up.
Thanks!
According to this SO question, it's simply not possible:
iOS Local Notification - Callback when in Background
Quoting part of the answer given in that link:
If your app was in the background or not running, and the user does
not tap the notification and summon your app, you will get nothing at
all and will have no way to know that the notification fired. This is
because notifications are not for your benefit but are a way of
sending a message to the user. (It sounds, from your question, as if
you may be misusing local notifications as a way of sending a message
to yourself. That is not what they are for.)

iOS Display local notifications pop-up while in background

I'm having a little problem in my app : I use to send local notifications to the user when actions are performed in my app, while it's running in background. Notifications are displayed perfectly in the notification center, but there is no pop-up when the notification occurs.
If I'm right, apps running in foreground can't display pop-up on the user screen (only in the notification center) but when the app is running in background, in fact there is pop-up displayed automatically ?
Thanks for your help !
EDIT : In fact, I'm speaking about a notification banner to be displayed too when the notification appear in the notification center.
When your app will go in background then you will see an immediate notification on your phone but you have to tap that notification to trigger didReceiveLocalNotification delegate.
If you receive local notification in foreground then didReceiveLocalNotification will be triggered automatically.
Above scenario is tested and verified.
Update: You must read this documentation: http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html

How to bring application to foreground in ios?

I am detecting for iBeacon in background. When my device comes in a particular region application should comes to foreground.
It cannot be done without user interaction. The only option is you can generate a push notification to tell the user to bring the application to foreground.
This is from the Apple documentation about this issue:
When the operating system delivers push notification (iOS or OS X)
and the target application is not running in the foreground, it
presents the notification (alert, icon badge number, sound). If there
is a notification alert and the user taps or clicks the action button
(or moves the action slider), the application launches and calls a
method to pass in the local-notification object or remote-notification
payload. If the application is running in the foreground when the
notification is delivered, the application delegate receives a local
or push notification.
To answer to some comments about WhatsApp, with it, when you receive a classic vocal call, IOS use CallKit to display your call and wake up your phone, but it's not inside app. I try to make a video call with WhatsApp, and in this case, there is a notification. Press notification open app and answer to the call.
Conclusion : It's impossible to wake up app from background to foreground in IOS, but it's not really a problem because you can use notification to display what you want and get the user to your app after a touch on your notification. All of iPhone users are familiar with this kind of interaction, it's better to deal with it.

detect why iOS app went into background, hardware home or incoming push notification

I want my app to behave differently if the user pushes hardware home button, versus other reasons it may go into background. I think I have sorted out the main cases, except I can't tell the difference between user tapping on incoming push notification from another app and hitting the hardware home. In iOS 5, I was getting an applicationWillResignActive when the notify first appeared, and then applicationDidEnterBackground when the user tapped. In iOS 6, I cannot find any event triggered by the appearance of a banner notification from another app. The applicationWillResignActive and applicationDidEnterBackground come back to back, just as in hitting hardware home. I tried listening to UIWindowDidBecomeVisibleNotification and other UIWindow notification, but nothing is fired. Any ideas?

Resources