UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"open app" message:#" backgr..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
How could I display alert when apps running background?
Use a UILocalNotification when your app is in the background.
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Your message";
notification.alertAction = #"Open app";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Related
Here is my code to set local notifications for Event but it shows different text for different iphones like , for some iPhone it shows "Slide for more" and for some it shows "Touch to open" where as i set "view details" message:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.alertTitle = self.titleCell.propertyValue;
localNotification.alertBody = self.titleCell.propertyValue;
localNotification.alertAction = #"view details";
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"This local notification"
delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];//use latest alert action
}
yourControler.m
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(#"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
you need register for local notification
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
I want to display notification popup when app is in foreground state, with alertbody as per code snippets.
It is completely working when app is in background state.
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification == nil)
return;
NSDate *dt = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate date]];
notification.fireDate = dt;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"After 10Secs...";
notification.alertAction = #"View";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
application:didreceiveLocalNotification method in your app delegate if you want to see the nofication while your app is in the foreground:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
//simply show a alert,but the standard one will not show up by itself
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
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
}
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];
}
}
I have this normal code for notification alert:
- (void)saveNotification {
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-20];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Evaluation Planner";
notif.alertAction = #"Details";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
- (void)showReminder:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:text delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
This works perfectly well for me but when I click onto "Details", I want it to be redirected to a particular page. Instead this only redirect to the main page of the application and I have to click through the whole app all the way to the reminder section to view it. I'm not exactly sure how to fix it. Do help me.
Thanks.
Your app delegate method application:didFinishLaunchingWithOptions: will get called with a notification payload. It's all documented well in Local and Push Notification Programming Guide.