How to detect push notification sent to other apps? - ios

I cannot seem to find a way to detect when a notification comes in. For example, if my app is open and the user gets a Facebook push notification\, is there any UIApplication notification or something else that will tell me that the app is no longer in the foreground and the Facebook alert is on top? Preferably something that is fired if the notification is a banner or an alert

You cannot detect a push notification sent to another app.
If you want to detect whether your app is not in foreground anymore, you can use the applicationWillResignActive: method of UIApplicationDelegate or register for the UIApplicationWillResignActiveNotification notification.
However, when the notification banner appears on top, your application is still in foreground. When the notification is prompted as an alert instead (the user can set this in the preferences), the application loses the focus and it won't be in foreground anymore.

The notification banner at the top is a window displayed by SpringBoard (another process) which does not become key until the user touches it. If the user slides the notification down and the notification center opens, your app becomes inactive and your app delegate hears about this. If the user taps on the notification, your app goes to the background and the other is open. If the user has elected to see alerts instead of banners, once the alert is displayed, your app will resign active. You can listen to UIApplicationWillResignActiveNotification notifications to know when the app resigns active.

Related

Handle push notification when app is running and notification tray is opened

While trying to implement FCM in my iOS app, I came across this issue. I have my app opened, at the same time I slide down the Notification tray of my device. When I send a push notification, my application state prints as INACTIVE (which is correct since the tray comes in foreground, leaving my app in the background)
I am loading a URL on notification tap, hence when user slides up the tray, the url is directly loaded and no notification is displayed in the tray.
In this scenario, I wish to display notification in the tray and open URL only when the user taps on the notification. Is there any way to detect if the Notification tray is visible over my app specifically?
you can use Third party library : https://github.com/bryx-inc/BRYXBanner or you can show alert by handling if app is in foreground or inactive state in notification recieving delegate.

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

Check if application was opened by tapping a notification

I am having some problems with the behavior push notifications in my app.
If the app is not running (not even in the background), when I touch a notification I can parse its payload and do a custom action, by reading the "launchOptions" dictionary.
When my app is running in the background, I am not being able to detect when a user foregrounds the app by tapping the notification, as application:didReceiveRemoteNotification:fetchCompletionHandler: is called immediately in the background, and when I foreground the app I have no way to check if it was done by tapping the app icon or the notification.
I know this is possible because Facebook's Messenger app does this - if I tap the notification it brings me to the chat, else it brings me to where I was previously.
Thanks.
If the user taps the app icon, application:didReceiveRemoteNotification:fetchCompletionHandler does not get called.
If they tap the notification, application:didReceiveRemoteNotification:fetchCompletionHandler does get called and application.applicationState will be UIApplicationStateInactive.
Check out this answer for a code snippet that you can use to quickly see this in action to have it make more sense.
Also, to achieve your goal be sure to do any updating of views when the application state is UIApplicationStateInactive, not UIApplicationStateBackground. The application state will be UIApplicationStateBackground when the push notification comes in. The application state will be UIApplicationStateInactive only when the user launches the app by tapping the notification.
I've seen a lot of answers with if statements like
if (application.applicationState != UIApplicationStateActive) { ... }
but that won't allow you to distinguish between a push notification being delivered and the user tapping a notification.

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.

PushNotification in iOs and Application icon is clicked

I receive PushNotification as badge as well as alert and are displayed as badges on the app icon.
I receive the payload successfully when I click on alert, which appears at top area of the screen.
But, if the notification is received and I click on the application icon i don't get the payload in any of below mentioned the methods.
application:didFinishLaunchingWithOptions:
application:didReceiveRemoteNotification:
So, what could be the problem or have I missed something in handling such case.
That would be expected behavior. You don't want to show the push notification for the user if he himself did not actively chose to to tap on the alert. If user is in your app while receiving notification you can ask the user if he wants to view it or dismiss it.

Resources