UILocalNotification display alert on notification center when app is in foreground - ios

I am working with UILocalNotification to notify users about their schedule. Everything is working fine but I'd like to know whether there is a way to display notification alert on notification center when the app is in foreground.
The alert will not be fire on running application main view.
Please help me.

Put this code in didReceiveRemoteNotification Method.
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSString *cancelTitle = #"OK";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Test"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:nil, nil];
[alertView show];
} else
{
//Do stuff that you would do if the application was not active
}

Related

How do you show an alert view for push notifications if the app is not in the foreground or background in iOS?

I know how to show a push notifications UIAlertView if the app is in the background or foreground using the following code:
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = [ [userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [ [UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
However, I also want to show an alert view if the app was completely closed and the user wants to open the app via the push notifications.
How can I achieve this?
Thanks
When application launches as a result of a push notification, in other words, when the app was not running, you need to handle that case in [AppDelegate application:didFinishLaunchingWithOptions:] method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:notification[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
If the app is in background or foreground (active) state, you need to handle that case in [AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] method.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:userInfo[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else if (application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) {
// Do something else rather than showing an alert view, because it won't be displayed.
}
}
For more information, you can take a look at Local and Remote Notification Programming Guide from Apple.

Get JSON payload data into a string from push notifications

Im trying to get my JSON payload data from a push notification into a string.
{
aps = {
alert = "BG push";
sound = ,
};
}
I researched on SO and on Parse and tried various ways including this Apple Push Notification with Sending Custom Data however my string reruns (null) or as in this example in the JSON format
I want the alert data "BG Push" in a string so I can put this in an alert view
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ( application.applicationState == UIApplicationStateActive ){
// app was already in the foreground
[PFPush handlePush:userInfo]; //<-----userInfo returns payload data in JSON format
}
else {
// app was just brought from background to foreground
NSLog(#"App was in background and opened from Push message");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Megger"
message: [NSString stringWithFormat:#"%#", userInfo]
delegate:self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil];
[alert show];
}
}
NSString *alert = userInfo[#"aps"][#"alert"];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Megger"
message: [NSString stringWithFormat:#"%#", alert]
delegate:self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil];
[alertView show];
How about
NSDictionary *temp = userInfo[#"aps"];
NSString *message = temp[#"alert"];
If it doesn't work, add this line and let me know what you get
NSLog( #"%#\r%#\r%#", userInfo, temp, message );

UILocalNotification presentNotificationNow Issue

this issue has been driving me crazy. What I am trying to do is trigger a notification when the user enter in a region. However, what I want is, if the user is using the app show an alert message and if the app is in background shows a local notification.
Here is my code:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive)
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Alarm" ofType:#"caf"]];
AVAudioPlayer *audioFile = [[AVAudioPlayer alloc] init];
audioFile = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:NULL];
[audioFile play];
UIAlertView *arrivingDestinationAlert = [[UIAlertView alloc] initWithTitle: #"Arriving" message:[NSString stringWithFormat:#"Alert"] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[arrivingDestinationAlert show];
}else if(app.applicationState == UIApplicationStateBackground || app.applicationState == UIApplicationStateInactive)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:#"%d to reach your destination", self.distToRemind];
notification.alertAction = #"Destination Approaching";
notification.hasAction = YES;
notification.soundName = UILocalNotificationDefaultSoundName;
[app presentLocalNotificationNow:notification];
}
[manager stopMonitoringForRegion:region];
}
What I was doing before was working. Which was lunch an local notification without asking whether the state of the app was background or active. I was just lunching the notification as soon the didEnterRegion was trigger. However now I can get it work.
Am I missing anything?
I think you are checking for the active state in the wrong spot. I do something very similar, but I check for active state in the AppDelegate when the notification fires. You should intercept the local notification in -didReceiveLocalNotification. If the state is active, push your UIAlertView there. If you fire the alert from the -didEnterRegion or -didExitRegion from the background, it will never be seen.

Iphone presentLocalNotificationNow not triggering alert and sound while app in background

I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert...
What can you do to always get the sound and the alert notification ?
this is what i have in my view
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = nil;
localNotif.hasAction = YES;
localNotif.alertBody = fbName;
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
and this is the app delegate
- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
return YES;
}
If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your application. In that case, you need to present the notification using presentLocalNotificationNow.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
[application presentLocalNotificationNow:notification];
}
}

Having issues in calling didReceiveRemoteNotification

I am using following code,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//code here to handle call
//[[UIApplication sharedApplication] openURL:
// [NSURL URLWithString:#"tel:1-408-555-5555"]];
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"Teshjhjkhhjkhjkhjkhkjhkhkhkjhjkhjkkkjhjhhjhjkjt" message:#"Test" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert1 show];
[alert1 release];
}
but when application is open I can see the alert, but I want this alert when I press the view button in push message.
Try to implement in this format:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification"
message:[NSString stringWithFormat:#"The application received this remote notification while it was running:\n%#",
[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}

Resources