I am running application for local notification on iPod Touch (iOS 7). Notification is coming but sound for the notification is not playing. I have placed sound file in folder and also checked sound settings on device. I tried with many sound files but nothing worked. Below is the code :-
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = #"alarm.wav"; ///UILocalNotificationDefaultSoundName;
// notify.soundName = #"../Documents/blabla.caf"
localNotif.applicationIconBadgeNumber = 1;
localNotif.alertBody = #"Staff meeting in 30 minutes";
// Specify custom data for the notification
// NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
// localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[self.tableview reloadData];
}
Some times this may occur because of the device. And try to clear the previous notification:
UIApplication* app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
Check this answer. This may help you solving the problem.
Please try first whether setting->notificationcenter->sounds is not off.Then check whether in setting->sounds->slider is not on zero.That means if u have sounds on zero that will not allow sound to come up.
Hope that helps.
Related
I'm using location service in background to update my app every day. In background it parses the XML and sets local notification at 18h, but only if parsed data is different then "No warning". But I'm getting every day notification with same alert body even when I shouldn't. This is how I push notifications.
If(![self.city isEqualToString:#"No warning"]){
// We only need 1 notification
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregCalendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponents setYear:[dateComponents year]];
[dateComponents setMonth:[dateComponents month]];
[dateComponents setDay:[dateComponents day]];
[dateComponents setHour:18];
[dateComponents setMinute:0];
UIDatePicker *dd = [[UIDatePicker alloc]init];
[dd setDate:[gregCalendar dateFromComponents:dateComponents]];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.alertBody = [NSString stringWithFormat:#"%# %#",self.date,self.city];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.repeatInterval = 0;
[notification setFireDate:dd.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
I am new to UILocalNotification concept.
I have a reminder button,by clicking it iam calling the UILocalNotification code......
-(void)LocalNotificationMethod{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = self.selectedDate;
NSLog(#" self.selectedDate %#", self.selectedDate);
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [_titleTextFieldObj text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
//UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
NSLog(#"notif %#",notificationArray);
//[cell.textLabel setText:notif.alertBody];
//[cell.detailTextLabel setText:[notif.fireDate description]];
//NSDate *datee=notif.fireDate;
//NSLog(#"notify date is %#",datee);
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
iam getting the alert.But i want to display the notification badge in that reminder button.
If i got two notification then i want to get the 2badge numbers in the button.
not in the app icon...
can anyone help me......
Try the CustomBadge, that builds and renders custom badges using Core Graphics. They're just UIView subclasses, so you lay them out using their frame just like any other UIView subclass.
You can add Custom badge in your application using CocoaPod
I am working on app which having functionality to show local notification.
In that, I set local notification for specific date after that I want to show it for next consecutively 84 days (12 weeks).For this I am using NSDayCalendarUnit repeat type. But those reminders are still showing after 84 days.
I tried - (void)applicationDidBecomeActive:(UIApplication *)application method to cancel notification but It needs at least one interaction with app after 84 days to Know the the system date.
Please give some solution or guideline on it. Following code I am using to set reminders.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:cDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:cDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [calendar dateFromComponents:dateComps];;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody =#"take your medicine";
notif.alertAction = #"View";
NSLog(#"SOUND=%d",[[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]);
if([[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]||cId>=100)
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval=NSCalendarUnitDay;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%d",cId]
forKey:NOTIFICATION_KEY];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
NSLog(#"Set reminder %d For Date =%#",cId,cDate);
}
This question already has answers here:
How to create local notifications?
(8 answers)
Closed 9 years ago.
I am creating an app by collecting 365 english "sayings".In my app i would like to use local notifications(everyday a sayings from my app should be notified to the user).i would like to use local notification ,not push notification.is it possible to notify the user with different sayings each day(365 days) .Can anyone help me please.....
There is no way to create 365 different local notification.
According to the documentation local notification is limited to 64 scheduled local notifications.
But you can update notifications, when user launch your app.
If content of notifications is the same, you can also schedule the notification for delivery on a recurring basis
Yes you can.Just create array with 365 dictionaries. dictionary keys should be id and saying.
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *datePickerDate = [NSDate date];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:datePickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:datePickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc]init];
[dateComps setYear:[dateComponents year]];
[dateComps setMonth:[dateComponents month]];
[dateComps setDay:[dateComponents day] + 1];
[dateComps setHour:[timeComponents hour] ];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:00];
NSDate *fireDate = [calendar dateFromComponents:dateComps];
//NSLog(#"%#",fireDate);
localNotif.fireDate = fireDate;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertAction = #"Alarm";
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:#"1",#"id", nil];
localNotif.alertBody = [saying with id 1];//you can use predicate here
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
in application delegate,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
int i = [[localNotif.userInfo objectForKey:#"id"] intValue] + 1;
localNotif.alertBody = [saying in index i ];
[localNotif.userInfo setValue:i forKey:#"id"];
}
If I schedule two UILocalNotifications and set them both to fire at the exact same fireDate. Then on the device (this is not the simulator bug) on the fireDate the application:didReceiveLocalNotification: will fire 4 times (2 times for each notification). Is this a known bug? Because I have not been able to find any information about it.
Please report the bug to http://bugreport.apple.com.
Having said that, it has been noticed before that while there is the bug in the simulator there also appears to be a bug on the device.
See the comments and answers on this SO question: local notification "didReceiveLocalNotification" calls twice
try this it's work in my application :
-(IBAction)setRemind:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//Gets our picker
NSDate *selectedTime = [datePicker date];
strDate2 = [dateFormatter2 stringFromDate:selectedTime];
NSDate *Date=[dateFormatter2 dateFromString:strDate2];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:Date];
// Set up the fire time
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
[dateComp setDay:[dateComponents day]];
[dateComp setMonth:[dateComponents month]];
[dateComp setYear:[dateComponents year]];
[dateComp setHour:9];
[dateComp setMinute:00];
[dateComp setSecond:00];
[dateComp release];
NSDate *date = [calendar dateFromComponents:dateComp];
[self scheduleAlarmForDate:date message:txtDescri.text];
}
-(IBAction)scheduleAlarmForDate:(NSDate*)date message:(NSString*)msg
{
//====== TO SEE OLD NOTIFI=======
UIApplication *Ap = [UIApplication sharedApplication];
NSArray *arr = [Ap scheduledLocalNotifications];
NSLog(#"Old Notifications :>> %#",arr);
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification *alarm = [[UILocalNotification alloc] init];
// Create a new notification
alarm.fireDate = date;
NSLog(#"fireDate IS >> %#", alarm.fireDate);
alarm.timeZone = [NSTimeZone localTimeZone];
alarm.alertBody = msg;
NSLog(#"msg IS >> %#",msg);
alarm.alertAction = #"Show";
alarm.repeatInterval = 0;
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.applicationIconBadgeNumber = 1;
[app scheduleLocalNotification:alarm];
[alarm release];
}
i Hope it's helpful to you.