localNotification issues - iphone

I am sending a local notification once a process is finished and it acts just fine.
This is my code for didReceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
CounterViewController *counterVC = [CounterViewController sharedInstance];
[counterVC notificationArrived];
}
But when the iPhone is locked those lines aren't called… What can I do to make them run in the background?

There are two method to receive local notification one is already you have implemented which is invoked while app is running & 2nd is in didFinishLaunchingWithOptions which is invoked while your app is running background you have add some code for receive local notification .....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
}
return YES;
}

Related

Disable UrbanAirship alerts

I want to ignore push notifications when the app is active. I am handling notifications as follows:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState != UIApplicationStateActive)
{
[[PushHelper shared] processPush: userInfo];
}
}
But when app is active and device receives push notification, the UIAlertView with notification message appears. How can I disable default handling from UA?
I had the same problem and found solution.
If define the delegate method displayNotificationAlert: of UAPushNotificationDelegate protocol with empty body, for example, then the automatic alerts will not be shown:
{
...
[[UAPush shared] registerForRemoteNotifications];
[UAPush shared].pushNotificationDelegate = self;
...
}
- (void)displayNotificationAlert:(NSString *)alertMessage
{
}
If you don't need to do anything with the push notification itself just remove the [[PushHelper shared] processPush: userInfo] from your code
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//nothing to do here
}
The didReceiveRemoteNotification method is only called when the app is running.

How to get notification.alertBody in my app from a remote push notification?

I just followed this tutorial Push notification and I successfully implemented the push notification for my iPhone app. Im able to get now the notification Details. However, I wanted to put the notification alertBody on a Label provided for notification alertBody.
I have a code in displaying the notification alertBody from a local Notification. But I know it is different from a push notification because it is used only for local notification.
on my AppDelagate.m
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(#"Recieved Notification %#",notif);
NSString *_stringFromNotification = notif.alertBody;
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:_stringFromNotification];
}
on my ViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:#"Notification" object:nil queue:nil usingBlock:^(NSNotification *note)
NSString *_string = note.object;
//Do something with the string--------
}];
}
It works perfectly on Local Notification but for push notification, It doesn't work. How to Implement this? Need your help please. I need to put the notification alert body at the Label or String.
first of all register for remote notifications in AppDelegate.m in method,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Invoke APNS.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
And then use following delegate method to recieve remote notification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received =%#",userInfo);////userInfo will contain all the details of notification like alert body.
}
Remote notifications run outside the sandbox the app is running in, so you can't capture the notification in the same way as local notifications, i.e. application:didReceiveLocalNotification. However, if the app is launched via the remote notification, you can capture the notification via the application:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// do something with the notification.alertBody
} else {
// from the springboard
}
}
If your application is already running when it receives a remote notification, your application delegate's – application:didReceiveRemoteNotification: method will be called; if the application is not currently running and is launched in response to the notification, the remote notification info will be put into the launchOptions dictionary in your – application:didFinishLaunchingWithOptions: method.
The method which you are implementing is for local notification only. If you want to handle push notification then you have to use method
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSLog(#"Received notification: %#", userInfo);
}
for the same. This method will be called if the app is on background only. If the app is not in background then you can fetch data in the following manner
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *notificationData = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if(!notificationData){
NSLog(#"App launched by tapping on app icon");
}else{
NSLog(#"Notification data -> %#",notificationData);
}
}

How to create a local notification which does not show an alert but only wakes up the app to perform a calculation? [duplicate]

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;
}

Push Notifications on iPhone App

I have successfully implemented push notifications to my app. The next step is, if the user receives a notification about this certain document, he/ she clicks it and it brings them to this document.
At the moment, I can receive the notification, but it only brings me to the app.
How can I do this?
Thanks.
I am not clear with your question. But you should handle push notifications in the following methods,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Handle here
}
and
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *tmpDic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (tmpDic != nil) {
//Handle here also
}
}

when application become active then how we load the another view or invoke the root view controller

i have load the mailcomposer on reciving the localnotification
is it possible??
You should code as follows,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
//code for opening mail composer
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//code for opening mail composer
}