I have built a test-application for local notifications. it looks like this
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = #"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = #"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
NSLog(#"Notifications: %#", [[UIApplication sharedApplication] scheduledLocalNotifications]);
My AppDelegate:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(#"%s", __PRETTY_FUNCTION__);
}
It works fine when I'm in Background or in the LockScreen I'm getting these notifications.
But when the first notifications comes in, it does not show the badge which should be "1" now. When the Second notification comes in the the badge shows "1" and not "2"(which would be the right number now). So what is wrong with my code?
You are making a simple mistake that you are scheduling the notification first than setting its badge number. So you should first set the notification badge number then schedule it.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = #"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = #"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"Notifications: %#", [[UIApplication sharedApplication] scheduledLocalNotifications]);
Also for the right badge number during the second notification either you should save your notification badge number or set it from the last notification badge as I had did in above code. Because in your code it just set the badge number from the current [UIApplication sharedApplication] badge which is 0 + 1 equals 1 each time.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = #"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 1; //first notification
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = #"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 2; // second notification
NSLog(#"Notifications: %#", [[UIApplication sharedApplication] scheduledLocalNotifications]);
after you add condition check first is fire then cancelallnotification and register remaining with decreasing badge order.
Related
I created a Local Notification that triggers 60 seconds when a certain button(SetButton) is clicked. My problem right now is if SetButton is pressed again, it does not override the first press, it displays 2 notifications and so on. How do I make sure that the second press of the button overrides the first press and there isn't a build up of notifications ?
- (IBAction)SetButtonPressed:(id)sender {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
My AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([UIApplication instanceMethodForSelector: #selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
if (localNotification) {
application.applicationIconBadgeNumber = 0;
}
}
If you only use notifications for that specific action you could cancel all notifications at once using
[[UIApplication sharedApplication] cancelAllLocalNotifications];
It looks like you only have one type of local notifications in your app.
In that case you could keep it simple. Whenever you want to schedule a new one - cancel the previous one.
- (IBAction)SetButtonPressed:(id)sender {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber =
[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
I had set the countdown timer for 1 hour 40 minutes and i want to set local notification for last 10 minutes and last 5 minutes remaining.
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5400];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
I had tried this but its not working.
You need set the notification fireDate
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:*YOUR 1 hour 30 minutes*];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
and
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:*YOUR 1 hour 35 minutes*];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Do like this,i hope, this will help you...
//Create Notification here
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = someTime;
localNotification.alertBody = #"Your time will expire in 10 mins";
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
Do same for last five min.
My time period is 60 you can change according to your choice.
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:**60**];
localNotification.alertBody = #"your message you want to show in notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
The code to register for notifications is:
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
And the code to schedule the notification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
[self setNotificationTypesAllowed];
if (notification)
{
if (allowNotif)
{
notification.timeZone = [NSTimeZone defaultTimeZone];
if ( [statusString isEqualToString:#"daily"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitDay;
}else if ( [statusString isEqualToString:#"weekly"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitWeekOfYear;
}else if ( [statusString isEqualToString:#"fortnightly"]) {
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*14];;
notification.repeatInterval = NSCalendarUnitMinute;
//notification.repeatInterval = NSCalendarUnitYear;
}else{
notification.repeatInterval = 0;
}
}
if (allowsAlert)
{
notification.alertBody = [NSString stringWithFormat:#"Do you really want to send message to %#",name];
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.alertAction = #"Yes";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showSMS) name:#"showSMS" object:nil];
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
I am able to repeat local notification daily & weekly but it is not repeating fortnightly ,please help
Yes as #Nikos suggested, try below code:
Cancel UILocalNotification.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Schedule local notification
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.userInfo = #{#"notification_identifier":#"After14Days"};
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
notification.alertBody = #"Text to display";
notification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
If you have any doubt then let me know.
To set a notification to fire every 14 days you have to create 26 separate UILocalNotifications with the repeatInterval set to NSYearCalendarUnit (the repeat interval must be set to a calendar unit, and there is no 14 days CalendarUnit in iOS).
Another way to handle it is to cancel the UILocalNotification in the didReceiveLocalNotification method and schedule a new one there for after 14 days. This method though assumes that the user interacts with the notification, otherwise the next one will never get scheduled.
When my app goes in background then applicationDidEnterBackground is automatically called and in this method we fire local notification. but didReceiveLocalNotification: method is not called
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Your expectations with local notifications are incorrect:
When your app will go in background then you will see an immediate notification on your phone but you have to tap that notification to trigger didReceiveLocalNotification delegate.
If you receive local notification in foreground then didReceiveLocalNotification will be triggered automatically.
Above scenario is tested and verified.
Update: You must read this documentation:
http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
Try this code:
NSDate *pickerDate = [date_picker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = #"Alert Body Message";
localNotification.alertAction = #"Alert Action Message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
and use this code in didFinishLaunchingWithOptions in App delegate class.
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
Before scheluding notification you must register it for specified type:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
// register notification settings
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
#endif
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
I am using the following code to make a local notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:30]; //itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = parentController.activeAssignment.notes;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I have checked using the method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
that the notification is firing, but is not showing up on my screen when the app is running in the background.