Local Notifications with iOS 8 - ios

Local notification are working well in foreground. but I need to perform some background work when a notification banner displays. It is working well when I tap on the banner when local notifications appear.
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(#"AppDelegate didFinishLaunchingWithOptions");
[self application:application didReceiveLocalNotification:notification];
}
return YES;
}
My problem is similar to this issue
I used code below to perform background task:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[[ReminderManger sharedMager] handleNotification:[[notification userInfo] objectForKey:#"reminderId"]];
});
}

There is no way to do that. Notifications are handled by iOS itself. If app would be handling this, there might be some way to get this figured out. I've been wasting my time over it but didn't succeed.
The only thing you can do is to get extended permission from iOS that your app need multi tasking in background. And then in background make your app become post local notifications, this will show banner whenever you want but you can't customize the banner. See Apple's line "An app that is running in the background may also schedule a local notification to inform the user of an incoming message, chat, or update." at this link
Now the thing is in background, you cant change your views, but parse data, and based on this you can make changes at time of launch.

Related

iOS: Programmatically dismiss a notification that is displayed on lock screen?

Imagine this: A user sees a notification on their lock screen e.g. "your server is online". Then something changes e.g. the server goes offline. Can I programmatically remove that notification (dismiss it) from the background even after it has been displayed on the lock screen?
Yes you actually can do this, you typically see it in action in messenger apps or social networking apps, for example, in some messenger app that has a web version, if you receive a message and you read it from the web but you already have received the push on your iOS app, when this happens you must send another push without display message, but a tag with value that indicates whatever you want:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([[userInfo objectForKey:#"reset"] boolValue]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
}
This is a silent push notification.

Understanding app background launch behavior

In apple Doc Understanding When Your App Gets Launched into the Background
Apps that support background execution may be relaunched by the system
to handle incoming events...
I am doing region monitoring and when I get that I am popping a UILocalNotification, but when I tap on UILocalNotification my app didReceiveLocalNotification is called. May be because my app is launched in background.
Second thing I did is I did not tap UILocalNotification and left for few minutes, means my app will terminate by iOS. I drag the notification center and then tap the UILocalNotification still my app enter in didReceiveLocalNotification.
The behavior I am expecting that app now launch in action of delayed UILocalNotification tap must enter in this method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(#"notification caused app to launch, alert body = %#", notification.alertBody);
// do what ever action you want to do
// you could just copy the code from "didReceiveLocalNotification" and paste it here
}
return YES;
}
What is actually happening can please any one explain in detail?
Even after a delay when I tap UILocalNotification app do not enter in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Your app isn't terminated after a few minutes of inactivity.It becomes idle, and only terminated if memory is needed.Remember that it is launched to process the region change in the background, and it sends the notification in the first place, so it's resident in the device memory in this stage.Try to explicitly quiting it using the multitasking interface or launch several memory consuming apps (games usually do the trick) to force it to quit.A few minutes of waiting in the background won't make it terminate.

Notifications in iOS

I have a requirement for an application, when it has multiple notifications and when the user clicks one of those, the rest of the notifications of the same app should still be visible in the notification centre. Normally what happens is those other notifications also get cleared. Is there a way that I can bypass this scenario?
Update - Is there a way that we can access received push notifications in the notification center?
Its done by iOS. Programmers have no control over it.
You can get the notification if the user click on the item in notification center and application is opening from it. Following is the code for it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//your notification will be in the dictionary if application is opening from notification center
return YES;
}

Is there anyway to know if users bring up app by clicking in notification center?

in method
didReceiveLocalNotification:(UILocalNotification *)notification i need to detect if it is invoked because user click on a notification in Notification Center or not to have appropriate actions.
Is there anyway?
using didReceiveLocalNotification:(UILocalNotification *)notification you cannot check if app is opened from local notification push notification or by directly clicking on the app icon.
But,
what you can do is in method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
write this code
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
// App opened from push notification but app was not in background
}else if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]!=nil){
// App opened from local notification and app was in background
}
and in method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
you can detect if app is opened from local notification when app was in background
and finally one last method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In this method you can check if app is opened from push notification when app was either running or it was in backround
I guess this will help u in knowing if app is opened from notification centre or from somewhere else

Launch Closed iOS App From Local Notification

When my iOS application is running in the background it responds fine to
- (void)application:(UIApplication *)application didReceiveLocalNotification:
(UILocalNotification *)notification
but when the application is closed it crashes and gives a SIGKILL error.
How can I run a method within the app if it is closed when the notification is received?
When you app is closed then when you get notification than on click of notification - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method is called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
// code here.
}
You can't run a method in the app when a local notification is received. The notification can provide any combination of an alert, icon badge number, and a sound (<30 secs).
You can run a method when it comes into the foreground again either through the notification or through other means.
When the app is in the background it will call applicationWillEnterForeground: prior to resuming. You can override this method to handle anything needed after the notification. You can override applicationDidEnterBackground: to determine when your app actually enters the background.
Method application:didReceiveLocalNotification: is called when the app receives a notification but is in the foreground. The alert, icon badge number, and sound will not be triggered when the app is in the foreground.

Resources