Trying to get push notifications work in foreground - ios

Am trying to make push notifications (FCM) show up when app is in Foreground. Multiple manuals are referring to the below method. I can confirm it is being called when message is received, but notifications do not show up when app is in foreground (only background).
//To show a notification when the app is in foreground
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog(#"completion handler, which is called but does not work");
completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
}

Related

launchOptions are nil after using custom actions on Push Notification

I'm using push notifications with custom actions on my iOS app.
It all works well when the app is in foreground/background, with my code inside - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler being called as expected.
However, if the app is not running, clicking on one of the custom actions will make the app open, but the call to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is being done with launchOptions set as nil.
If I click the notification itself, instead of one of the actions, it works as expected as well!
Any idea what is going on here?

UIUserNotificationSettings Alert Causes App to Resign Activity

I currently have an application that requires Local Notifications, so naturally I must ask the user if he or she would like to 'Allow Notifications'. Here is how I am doing this:
AppDelegate.m
// Local Notifications
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
At the initial loading of the app, I have a AVAudioPlayer that begins playing. I have it set up so that when the user leaves the app (the application enters the background) the music fades out and pauses.
The problem is that during the first time the app is launched, when the notification pops up asking for the user to allow notifications, it appears that the app is fooled into thinking it is about to resign activity. I can't seem to figure out how to either avoid JUST the notification alert from triggering this, or at worst "bring the app back", and becoming active once the alert is dismissed with one of the 2 notification options.
Here is what I am calling in my ViewController to notify the app when it changes state:
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseAudio)
name:UIApplicationWillResignActiveNotification object:nil];
...
}
If someone could tell me how to ignore the notification alert so that it does not think it will be resigning activity, OR how to bring back the application after it does "resign". I currently have these other notification handles in attempt to bring it back, but they are not called after the alert "messes everything up":
(in the same ViewController within viewDidLoad)
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(animateLoginScreen)
name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeAudio)
name:UIApplicationDidBecomeActiveNotification object:nil];
I appreciate any help on this one. Thanks!
Whenever a "system dialog", such as the notification permission dialog, is shown your application does resign active because it is iOS, not your app, that is responsible for processing the permissions request.
Your app isn't 'fooled', it really is resigning active for the period of time that the dialog is displayed. I suspect that you should pause your audio on your application entering the background, not merely on it resigning active. UIApplicationDidBecomeActiveNotification should be posted if you are getting a willResignActive though. Do you get a call to the corresponding AppDelegate methods?

How to determine app is in active state or inactive state ios?

I need to know when an App is in Foreground, it is in active state or inactive state ?
If my App is in inactive state I need to fire the Logout Protocol and destroy the current user's session,
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"App is not active logout success");
}
Is there any appDelegate method which tell me that app is in inactive state, any code example will help me a lot.
If need work with "NSNotificationCenter", in which class can I add the code and who will be observer.
To test for the state you can do something like:
[[UIApplication sharedApplication] applicationState]==UIApplicationStateInactive
or
[[UIApplication sharedApplication] applicationState]==UIApplicationStateActive
If you want to be notified you can do:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(yourselector:)
name:UIApplicationDidBecomeActiveNotification object:nil];
or
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(yourselector:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
You can do other notifications too (from https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/):
UIApplicationDidBecomeActiveNotification
UIApplicationDidChangeStatusBarFrameNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIApplicationDidEnterBackgroundNotification
UIApplicationDidFinishLaunchingNotification
UIApplicationDidReceiveMemoryWarningNotification
UIApplicationProtectedDataDidBecomeAvailable
UIApplicationProtectedDataWillBecomeUnavailable
UIApplicationSignificantTimeChangeNotification
UIApplicationUserDidTakeScreenshotNotification
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationWillChangeStatusBarFrameNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification
UIApplicationWillTerminateNotification
UIContentSizeCategoryDidChangeNotification
If you want to use the app delegate, you can use:
- (void)applicationDidEnterBackground:(UIApplication *)application {}
or
- (void)applicationDidBecomeActive:(UIApplication *)application {}
Please refer this Apple Doc :App Life Cycle
applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.
applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.
applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.
applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended

Detect when fire of local notification

There is a way to detect when a localnotification is fire??
For example i have one notification that is fire 12:00 a.m it show the notification there is way to know if the user touch the notification.
Because if the user no touch the notification i want to set other alarm to fire at 20 minutes after if it not touch the notification when is fire.
Call -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification in the app delegate to know when the user returns to the app from a notification. Note: this is only called if the application was not closed, i.e. closed via multi-tasking. I'd suggest setting a timer for 12:00AM to set another notifcation and if the user returns to the application from the first notification then cancel the second one.
while application in forground or background then this method call when fire notification in appDelgate
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
}
if application close then click on notification then didFinishLaunchingWithOptions method check is local notification
if ([[launchOptions allKeys] UIApplicationLaunchOptionsLocalNotificationKey]) {
}

notification launch custom method/class

just want to make sure i'm on the right path.
I'm creating a local notification as an alarm clock. when the user hits button... I want it to DO STUFF.
I'm thinking I have to call my method in
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
.. .for when the application is closed
(void)applicationWillEnterForeground:(UIApplication *)application
... for when the application is in the background... when user hits okay
Is there a better way to do what I'm trying to accomplish... DO STUFF when user hits okay on notification?
thanks in advance
According to the documentation of local and push notifications, you should call application:didFinishLaunchingWithOptions: in both cases :
Handling Local and Remote Notifications
Let’s review the possible scenarios when the operating delivers a
local notification or a remote notification for an application.
The notification is delivered when the application isn’t running in the foreground.
In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its
delegate’s application:didFinishLaunchingWithOptions: method (if
implemented); it passes in the notification payload (for remote
notifications) or the local-notification object (for local
notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about
the notification .
They can not be forced to call. only Monitoring.
UIApplicationDelegate Protocol
These methods provide you with information about key events in an
application’s execution such as when it finished launching, when it is
about to be terminated, when memory is low, and when important changes
occur. Implementing these methods gives you a chance to respond to
these system events and respond appropriately.
If you want you're ViewController get Notification there method. try this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidFinishLaunching:) UIApplicationDidFinishLaunchingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
- (void)applicationDidFinishLaunching:(NSNotification *)noti
{
//do stuff
}
- (void)applicationEnterForeground:(NSNotification *)noti
{
//do sutff
}

Resources