Unable to act on notification after app is killed - objective-c

When my app is in foreground or inactive then upon receiving push notification, I can click on it and the desired action occurs. If I kill the app then upon tapping a notification the app comes to the home screen. Handling Push Notifications when App is Terminated from this post I understood that this is the desired behavior.
I also have 2 actionable buttons attached on my notification. if I click on one of them then I am not redirected to the home screen and the app loads indefinitely! Is there a work around to make the user come to the home screen?
Methods implemented by me which dont get executed after the app is killed and i receive a notification
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
Also, I read on the posts that after the app is killed and if I click on a notification then didFinishLaunchingWithOptions is executed. I added logger statement in my method but i dont receive them.
Any pointers are appreciated.

Related

Wait when open app after push notification

When user get a notification when the app is in background , than he push the notification message in home screen , than when the app is open this one is being called :
//on delegate class
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
Than here, i want to perform something , than wait for his delegate here, only than to let the app become active again, how can i eliminate the app from starting, only when i am ready for it ?

how to handle push notification if application is in inactive mode?

how do we handle push notification if app is in background mode? means i want to get push notification alert message when I am reopen the application after got the push notification alert.
can I do this ?
I am using this method when my my app is in active mode.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
But when the app is not active, so how to get this?
You don't. Your app is not guaranteed to receive notifications. If the user taps the "open" button then your app will start with the dictionary passed in as the startup parameters in application:didFinishLaunchingWithOptions:. Of course, you users might tap cancel instead.
If the user taps the notification then only trigger to get the notification payload (or) userInfo dictionay value. which using below code in appdelegate file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
}
}

UILocalNotification when app is closed

Using the UILocalNotification when the app is open, this function in the app delegate is fired :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
but when the app is close and not in the background, it starts the app when i hit the notification massage, but it doesnt fire this method.
i need to fire it because she is the one who take me to another scene-that i need to present when someone get the notification.
it works only when she is on background .
You have to implement application:didFinishLaunchingWithOptions:. The notification will be one of the options.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// handle your notification here.
}
}
From the specs:
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).
In other words, application:didReceiveLocalNotification is only for when, as you've found, the app is running.
If the app is LAUNCHED due to a local (or for that matter remote) notification, the goods from the notification are passed into the application:didFinishLaunchingWithOptions: method, and that's where you catch that.

Local notification show uialertview while app in background

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
The specific function is called when the app is in foreground. How can i show a local notification when the app is in background? I press the home button and then i want to show an uialertview to the user...is this possible?
Any help appreciated
if app isn't running in the foreground..then the notification is shown as a ALert View by default..

UILocalNotification

I have UILocalNotification the has two buttons a cancel and view button when the application is in the background and the alert come up I click the view button and it opens on application but I have a method call that is ment to run if the launch option has a UILocalNotification object that is not working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//notifcation key
UILocalNotification *notifcation = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notifcation) {
NSLog(#"working");
}
}
when your application is in background then didFinishLaunchingWithOptions will not call in that case use -
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
here userInfo is same as UIApplicationLaunchOptionsLocalNotificationKey.
Thanks for Help I have worked it out I thought that the launch options were passed to the application even if the app was in the background but that is not the case if application is in background and a local notification comes in the application delegates has the didReceiveLocalNotification method called I thought that was call when application was in foreground and when in the background application did launch with options was called but the options only has the local notification key when the app is closed (not in background) and the notification is call to start up the application.
so if ay one else has te same issue make sure you use
-(void) application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
method when you click your action button on the local notification alert to run and actions when you open your application from that notification alert button.