UILocalNotification - objective-c

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.

Related

Change Action When Push Notification Swiped

Is there a way to change the action when a user swipes on a push notification from your app? For example, a push notification that shows the text "Check out our new app!", then if the user swipes launch the App Store to that app. Does anyone know if there is a way to do this? If yes, could you please do it in Swift preferably, if in Objective-C it's fine I should be able to convert it.
Thanks in advance
When user swipes or opens application from top popup, then -application:didFinishLaunchingWithOptions: is called with push notification options.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
// So check here if push notification says "Check out our new app!"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://itunes.apple.com/app/crosswords-classic/id653530195"]];
}
}

Local Notification To Load a View

I have an app that using local notifications, I want to have the app load a different page when a notification is recived. I also want it to load this new view when the slide to unlock over the notification is used.
My storyboards do not have a navigation controller is this possible to be done?
You can handle local notifications in the following ways:
Add in following method located in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
This code:
// Handle launching from a notification
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
// redirect to UIViewController
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:[NSBundle mainBundle]];
UIViewController * notificationViewController = [storyBoard instantiateViewControllerWithIdentifier:#"IdentifierOfViewController"];
self.window.rootViewController = notificationViewController; // set the new UIViewController
}
This allows you to handle LOCAL notifications as soon as the app is launched, either by acting on a 'slide to view' dialog or just by opening the app.
You can then change the rootViewController in the application if there is a notification
My storyboards do not have a navigation controller is this possible to
be done?
Yes, this wouldn't be a problem as long you use modal navigation.
EDIT
If you want to handle the notifications if the app is currently opened by the user you can catch the notification, by placing the following method in the AppDelegate:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

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