I have created a UILocalNotification with
localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:+60
So, After 60s, it will notification for me.
And my AppDelegate, I have call method.
(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
NSLog(#"OK...");
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
When I click on notification, it is called didReceiveLocalNotification method. It is OK.
But when I click in App's Icon in screen, it is not called didReceiveLocalNotification method.
I have research and followed this code in didFinishLaunchingWithOptions
// Handle launching from a notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// If not nil, the application was based on an incoming notifiation
if (notification){
NSLog(#"test...");
}
But, it is not working.
It is get notification = nil.
So, it is not NSLog(#"test...");
Please help me!
when app icon is clicked its not local notification click, where as your code in didFinishLaunchingWithOptions works in the case when app is not running in background and user or you click on local notification. Whereas whenever app icon is clicked not always didFinishLaunchingWithOptionsis called, it is called only when app starts i.e not present in background but, applicationDidBecomeActive is called whenever app starts from anywhere in any mode. UIApplicationDelegate [https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol]
Related
I'm using Objective-c to create a UILocalNotification in my iPhone app. I'm targeting iOS 8 and using XCode 6.
My problem relates to handling UILocalNotification when app is not running and it's opened by tapping on a notification. When the user taps the notification and opens the app I use didReceiveLocalNotification in AppDelegate.m to show a particular View Controller and send the VC some data (a date).
This works fine when tapping the notification from the lockscreen. When tapping the notification in the Notification Center didReceiveLocalNotification is never called. I used a UIAlertView to test this on my device. didFinishLaunchingWithOptions is called, but when I include the code to show the particular View Controller in that method it never works (though another UIAlertView showed me it was running that part of the code, just never completing the showViewController method).
Here's my didFinishLaunchingWithOptions code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
// Handle local notification received if app wasn't running in background
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([[notification.userInfo objectForKey:#"notification"] isEqual:#"mood rating"]) {
// Create reportVC
NSLog(#"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window.rootViewController showViewController:reportVC sender:self];
}
} else {
[self createLocalNotification];
}
return YES;
}
And here is my didReceiveLocalNotification code:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Create report view
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"reportVC"];
// Same date stuff goes here as in didFinishLaunchingWithOptions
// Show report vc
[self.window.rootViewController showViewController:reportVC sender:self];
}
The date stuff that I've take out is just checking if it's after 9pm or not, and either creating today's date or yesterday's, and then setting the result as a property of the reportVC. Let me know if that's relevant and I'll add it back in.
So here's some stuff I've tried to fix this:
I've tried using presentViewController:animated:completion: instead of showViewController:sender: but I want to use showViewController so I can have the navbar show up, and that didn't fix the problem anyway.
I've tried adding this line to my didFinishLaunchingWithOptions method:
[self application:application didReceiveLocalNotification:notification];
which did kind of fix the problem—when tapping from Notification Center it opened the right view, but it ruined lockscreen notifications. With this line added, when tapping a notification from the lockscreen I got the reportVC presented twice: the first one was a black screen apart from the navbar, and the one on top of that was correct.
I tried replacing my current showViewController line of code with this instead:
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
[topController showViewController:reportVC sender:self];
but that didn't fix the problem either.
I tried taking the code I've doubled up on out of my didFinishLaunchingWithOptions method, because I realised didReceiveLocalNotification seems to be run anyway once didFinishLaunchingWithOptions is done. That does seem to be the case, but it doesn't fix my problem with Notification Center notifications.
And in case this is screwing it up, here's how I'm testing this at the moment:
I add these two lines to the didFinishLaunchingWithOptions method:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
I change the scheduled time for the notification to be about 3 minutes into the future. It's normally set to go off at 9pm every night using date components like this:
dateComponents.hour = 21;
dateComponents.minute = 0;
And I change the notification's repeatInterval to be NSCalendarUnitMinute instead of NSCalendarUnitDay which is what it's set to for release builds.
Then I run the app on my device using XCode, and stop it once it's run and scheduled the notification. I run it again without these two lines:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
And then stop the app from XCode, open multitasking on my device, swipe the app up to close it, and wait for a notification to arrive. After tapping each test notification I multitask and close the app again so I can test from a totally closed app each time.
You may want to try this (present your view controller asynchronously with the dispatch_async()):
if ([[notification.userInfo objectForKey:#"notification"] isEqual:#"mood rating"]) {
dispatch_async(dispatch_get_main_queue(), ^{
// Create reportVC
NSLog(#"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window.rootViewController showViewController:reportVC sender:self];
});
}
or this (call [self.window makeKeyAndVisible] before presenting your view controller):
if ([[notification.userInfo objectForKey:#"notification"] isEqual:#"mood rating"]) {
// Create reportVC
NSLog(#"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window makeKeyAndVisible];
[self.window.rootViewController showViewController:reportVC sender:self];
}
First I want to know, which specific method is called when i tap on local notification. I want to open a url upon tap on notification. below is code app delegate.
Now the issue is, url opens automatically even if i don't tap on notification. Please guide me if u know that. Thank you
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
{
application.applicationIconBadgeNumber = 0;
NSLog(#"Notification tapped :) ");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com.pk"]]; }
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
This methos is called every time when notification fire.
To open url when tap on notification you have to check state of the app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
appState = application.applicationState;
if (appState == UIApplicationStateActive)
{
// Don't open Url.
}
else
{
// Open Url.
}
}
Have a look at the Local and Remote Notification Programming Guide from Apple, section "Handling Local and Remote Notifications".
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).
Later is says
[...] get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
Try to check application.applicationState. If the app isn't active in the foreground , then not open URL.
application: didReceiveRemoteNotification: fetchCompletionHandler:
is different from
application: didReceiveRemoteNotification:
How? from the docs:
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running, the system calls this method
regardless of the state of your app. If your app is suspended or not
running, the system wakes up or launches your app and puts it into the
background running state before calling the method. If the user opens
your app from the system-displayed alert, the system calls this method
again so that you know which notification the user selected.
My struggle is: I want to know if the method was called by the user tapping an a system-displayed alert from the Notification Center or from a silent push notification that wakes up the device. Currently, as far as I can see, there is no obvious way to differentiate.
- (BOOL)application: didFinishLaunchingWithOptions:
Tracking the launchOptions in the above method is not a solution because it's only called if the app is suspended/not running in background. If it's running in the background it doesn't get called.
The Apple docs are a bit confusing
application: didReceiveRemoteNotification: fetchCompletionHandler:
is used if your application supports the remote-notification background mode (ie you're doing BackgroundFetch.)
application: didReceiveRemoteNotification:
is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
You can check the UIApplicationState to see if the app was brought to foreground by the user (tapping on notification) or was already running when notification comes in.
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
// user tapped notification while app was in background
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
// go to screen relevant to Notification content
} else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}
You could check the UIApplication's applicationState to distinguish silent calls from calls made with the application being actively used by the user:
typedef enum : NSInteger {
UIApplicationStateActive,
UIApplicationStateInactive,
UIApplicationStateBackground
} UIApplicationState;
Or keep your own flag set on the delegate's applicationDidEnterBackground:.
Application State is not reliable because if you have control center or Apple's notification center open over your app, application: didReceiveRemoteNotification: fetchCompletionHandler: will get called and the application state will be Inactive.
I'm having the same issue trying to respond to a click on the notification while the app is in the background and there doesn't seem to be a reliable way to solely identify this.
In case of swift
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let state : UIApplicationState = application.applicationState
if (state == .Inactive || state == .Background) {
// go to screen relevant to Notification content
} else {
// App is in UIApplicationStateActive (running in foreground)
}
}
In iOS 10.0+ you can use the method
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;
to detect when user taps on a system-displayed alert from the NotificationCenter.
If you implement the method above
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler; will be called only when a notification is received
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler; will be called when user taps on notification
When application: didReceiveRemoteNotification:fetchCompletionHandler: method is called app state is UIApplicationStateInactive if user taps on alert (in this case you would like to prepare some UI) and is UIApplicationStateBackground when app is silently woken (in this case you just load some data).
I'm not sure if I understand your question.
Do you want to differentiate between a silent push notification background fetch and a noisy push notification? You can simply check whether the push notification dictionary contains the "content-available" key: [[userInfo objectForKey:#"aps"] objectForKey:#"content-available"] If it does, then it should be a silent push. If not, it was a normal push.
Do you want to know if that background fetch method is called when the application receives a notification and it is in suspended/not running? If so, you can do the following:
Download and import LumberJacks into your app. Look through the directions and learn how to set it up such that you can save logs to the disk.
Put this in any method you want to see whether/when that method is invoked:
DDLogDebug(#"%# - %#",NSStringFromSelector(_cmd),NSStringFromClass([self class]));
This will print the class and the method to the log file.
Examine the log file after sending yourself a push notification to your background-fetch enabled app, and see if any of the methods get called by looking at your log file.
If you have set up your app correctly for background fetch, the method application: didReceiveRemoteNotification: fetchCompletionHandler: will be called even when the app is backgrounded/not running if you receive a push notification (silent push or not).
For Swift: In application(_:didFinishLaunchingWithOptions:) parse the application options. If they exist, you know the app was launched from them tapping.
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
print("Remote notfi is \(remoteNotif)")
if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
/// - parse notification
}
}
Otherwise, you can handle the tap in, and you know that the app is open/background/inactiveapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
I am using local notification in my app, the only thing i care about is what exact notification the user clicked.
the method
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
triggered when the notification is received but i need to handle the notification when the user clicked on it so this is useless to me.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
triggered only when app was not running, also not what i need
i searched the net but found only handles if the app is active or not running. how can i handle it when the app is on the background?
what i need in other words is to recognise the exact notification that the user clicked when the app was running on the background
When creating your UILocalNotification, you can set the userInfo to set any associated data/unique identifiers.
Ex.
UILocalNotification *someNotification = [[UILocalNotification alloc] init];
[someNotification setUserInfo:#{ kSomeUniqueIdentifierKey : #"identifier" }];
and then,
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([notification.userInfo[kSomeUniqueIdentifierKey] isEqualToString:#"identifier"]) {
// We know what notification was responded to based on userInfo
}
}
The above method fires either immediately upon receiving the notification while the app was running or when the user taps the notification that fired while your app was in the background.
If you want to ignore these notifications while the app is running, you could always check the state of the application to determine if it's responding to the notification while running or in the background.
I am working with iOS 9, and the solution is to check the launchOptions within the AppDelegate didFinishLaunchingWithOptions. As follows:
// Were we launched from a local notification?
if let lo = launchOptions
{
if let ln = lo[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
{
// Do our thing...
}
}
I would like to know, if it is possible to somehow "wake up" a task that is in the background, to quickly check something on the network.. I think that this could be done with UILocalNotification, however, no matter what I tried, I could not get the didReceiveLocalNotification to do ANYTHING when the app is in the background.. After starting up, I immediately close the app by pressing the Home button (there is a 10 second delay for local notification to fire). This code works PERFECTLY when the app is in the foreground, and just kind of sitting there...
In app delegate header file:
UILocalNotification *localNotif;
For testing, I set up local notification to fire quickly in the appDelegate startup.
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"setup the timer for 10 seconds");
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
NSLog(#"getting kicked");
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
NSLog(#"INACTIVE..");
} else {
NSLog(#"ACTIVE..");
}
}
The user has a couple of choices: #1) Do they want to see a notification for your app. #2) If notifications are enabled for your app, do they want to click on your notification to launch your app. If they do accept notifications and open your notification while your app is in the background, application:didReceiveLocalNotification is called. To be clear, the user has to accept the notification (such as sliding the slider underneath the notification)... otherwise NOTHING is called.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"%#", notification);
}
If your app has been terminated application:didFinishLaunchingWithOptions: is called -
- (BOOL)application:(UIApplication *)
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
UILocalNotification *theNotification =
[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(#"%#", theNotification);
return YES;
}