Local Notification in foreground in iPhone SDK - iphone

Will the local notification show up when the app is in foreground and currently running in iPhone SDK?

No, you will receive a the notification in the appdelegate.
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
//Place your code to handle the notification here.
}

I made an lib to make an animation almost as same as local notification's.
Check this:
https://github.com/OpenFibers/OTNotification
Demo:
And you can post a new message to this lib when you received a message in
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
OTNotificationManager *notificationManager = [OTNotificationManager defaultManager];
OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init];
notificationMessage.title = [self notificationTitle];
notificationMessage.message = #"A notification. Touch me to hide me.";
[notificationManager postNotificationMessage:notificationMessage];
}

The accepted anser is right, but it's not enough to receive all notifications and show something to user from
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
You have to check, is it current notification or not.
Sometimes there is fires another notifications (when you cancel them, for example). So, you have to check, that is what you except:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f)
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Notification alert", #"")
message:notification.alertBody
delegate:self
cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
}
}

if your app is currently in foreground the following function will be called in your Delegate:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation
you can then decide to show an alertview, but the standard one will not show up by itself

Swift 2.2:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
var state = application.applicationState
if state == .Active {
// handle the notification, e.g. show an alert
}
}
Swift 3.0:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
var state: UIApplicationState = application.applicationState
if state == .active {
// handle the notification, e.g. show an alert
}
}

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

localNotification issues

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

UILocalNotification when the application is active?

If my App receives a UILocalNotification when it is active, I want to show my own UIAlertView and dismiss the system alert. Here's what I'm doing:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alert = ...
[alert show];
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
My own alert shows up, but the system alert somehow remains in the system and is shown as soon as I exit my App.
What am I doing wrong?
Instead of setting [[UIApplication shareApplication] cancelLocalNotification:notification];
set it as [notification setFireDate:nil];.

Apple Push Notification Service using Urban Airship in iPhone

I have implemented Apple Push Notification service using Urban Airship and successfully received the notification in my application. If i receive the notification the alert view comes, If i click the view button in alert view, it goes to start the application. Generally it happens in APNS. But my client wants, If any updates happened in the RSS feed and alert view comes, If we click the view in alert view, it should go to the particular feed in the application, doesn't start the application. SO is it possible to do that?. Is any possible to write the events for the particular alert view buttons in my application.
Here my sample code is,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
[window addSubview:viewcontrollers.view];
[window makeKeyAndVisible];
NSLog(#"remote notification2: %#",[launchOptions description]);
return YES;
}
In this method didFinishLaunchingWithOptions, i cant get the dictionary values and it always get the Null Value. Is any possible to get the dictionary value in this method(Notification comes).
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *message = [userInfo descriptionWithLocale:nil indent: 1];
NSLog(#"The message string is %#",message);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Remote Notification" message: message delegate: nil cancelButtonTitle: #"ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
In this method, i could get the dictionary value. But this method calls only,if any update happens while running in the application.
Please guide me!
Thanks
It is indeed possible to do that. In your application:didReceiveRemoteNotification method you'll be passed an NSDictionary with all of the push notification's data. What you'll want to do is send some ID or URL in the payload to Urban Airship. Something like:
{
"aps": {
"alert": "New RSS entry"
},
"entry_id": "XYZ123"
}
And then you can write code to go and fetch the proper feed entry in your application.
When the application isn't running or has been terminated by the system and the app is launched via the notification:
In this case you have to get the notification dictionary (which is itself a value of the launchOptions):
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006786-CH3-SW18
I imagine the code would be something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary *remoteNotification = (NSDictionary *) [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification != nil) {
NSString *message = [remoteNotification descriptionWithLocale:nil indent: 1];
}
}