I give a custom sound to local notification, but it is not working.
notif.soundName = #"sound.caf";
it's play default sound why?
Thanks.
Maybe you don‘t add the sound file (*.caf) in Xcode project: Build Phases/Copy Bundle Resources.
I have done notification successfully with the code below.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
/* NSDateFormatter *formatDate=[[NSDateFormatter alloc]init];
[formatDate setDateFormat:#"MM/dd/yyyy"];
[formatDate setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *strDate=[formatDate dateFromString:txtDate.text];
*/
localNotif.fireDate = itemDate;
//localNotif.timeZone = [NSTimeZone systemTimeZone];
NSLog(#"the item date is %#",localNotif.fireDate);
// Notification details
//localNotif.alertBody = [eventText text];]
localNotif.alertBody=strEvent;
// Set the action button
localNotif.alertAction = #"View";
//localNotif.soundName = #"Acoustic Noodling 01.caf";
localNotif.soundName=#"alarm1.aif";
About playing custom sounds in Local notifications
Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4
Assume that notification is an object of type UILocalNotification.
notification.soundName = #"sound.caf";
If this doesn't work initially then you may refer to this post and there in refer to This Answer That tells us to use sound file of the correct format (i.e. either Linear PCM or IMA4).
Just use relative path of your file rather than providing absolute or full path.
For example:
localNotification.soundName = #"../Documents/recordedFileName.caf";
Also, keep in mind that recordDuration of file should be less than or equal to 29 seconds.
That's it, hope that answered.
Related
I want to have a few local notifications with no display and no sound, but only a vibration. I can get it to do no display, by simply setting alertBody to #"", but how can I send no sound? I am thinking if I don't get a better way from you guys, that I will be able to just make a sound that is empty, add it to my project, then set soundName to that sound. But is there any sort of default way to do this?
If I add a phony sound name it still plays the default notification sound.
Thanks!!
No you can not disable sound because UILocalNotification does not provide any option for this. So better option is as you told in your question to use a empty sound file.
Yes you can add another sound file.
NSString *soundFile=#"temp.mp3";
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
if (localNotification==nil) {
return;
}
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.soundName = soundFile;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
In above code just put the name of sound file which you have saved in your resources in place of "soundFile" string.
You can not set sound.or
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)responsewithCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNCalendarNotificationTrigger class]]) {}
else{}
//don't write UNNotificationPresentationOptionSound
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
Local Notification with no sound
try below this code
let content = UNMutableNotificationContent()
content.sound = nil
I am working on App which will set an alarm on ios for a time depending on user input.
Meaning: if a user selects row 1 of table then it will look into dictionary (which may say 20 minutes),,, then it should set an alarm in ios for (currrent time+ 20 minutes).
Can someone please tell me the best way to approach this.
You can use UILocalNotification:
UILocalNotification *local = [[UILocalNotification alloc] init];
// create date/time information
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:20*60]; //time in seconds
local.timeZone = [NSTimeZone defaultTimeZone];
// set notification details
local.alertBody = #"Alarm!";
local.alertAction = #"Okay!";
local.soundName = [NSString stringWithFormat:#"Default.caf"];
// Gather any custom data you need to save with the notification
NSDictionary *customInfo =
[NSDictionary dictionaryWithObject:#"ABCD1234" forKey:#"yourKey"];
local.userInfo = customInfo;
// Schedule it!
[[UIApplication sharedApplication] scheduleLocalNotification:local];
[local release];
i want custom sound like runtime recorded message should be played for Alaram alert
but how we can it be possible ?
Sure.
UILocalNotification *ln = [[UILocalNotification alloc] init];
ln.soundName = [NSString #"mysound.wav"];
...
I have an application in which i have button. When i click on button then generate a local notification. The variable of local notification is set in appDelegate file. For generate local notification i used this code:-
UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.alertBody = #"Time for another cup of coffee!";
ln.applicationIconBadgeNumber = 1;
ln.fireDate = notification_date; //[NSDate dateWithTimeIntervalSinceNow:15];
ln.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSString *string_date=[formatter stringFromDate:notification_date];
NSDateFormatter* formatter_alarm = [[[NSDateFormatter alloc] init] autorelease];
formatter_alarm.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter_alarm setDateFormat:#"hh:mm a"];
NSString *str=[formatter_alarm stringFromDate:notification_date];
appDelegate.alarm_time=[NSString stringWithFormat:#"%#",str];
NSLog(#"%#",appDelegate.alarm_time);
[[NSUserDefaults standardUserDefaults] setObject:appDelegate.alarm_time forKey:#"alarm_on_time"];
[[NSUserDefaults standardUserDefaults] setObject:string_date forKey:#"alarm_on_date"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"alarm will activate on%#",notification_date);
ln.soundName = #"alarm.wav";
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
ln.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
// if(appDelegate.appDelegate_notification ==nil)
// appDelegate.appDelegate_notification= [[UILocalNotification alloc] init];
appDelegate.appDelegate_notification=ln;
[ln release];
Now i have a another button which is used for change the sound of local notification. I wan t that when user click on that button then change the sound of local notification. For that purpose i use this code:-
appDelegate.appDelegate_notification.soundName = #"Blow.wav";
Now problem is that when i click on another button then sound of local notification is not changed. How make that event on button click?
Thanks in advances...
When we generate any local notification then we can set sound file at that time . If we want to set sound after setting notification then i get only one way that i share here. When we edit any sound of alert then first we have to save fire date of alert and after that we will cancel that alert and set a new alert with same fire date anad this time we will pass new sound file. I use this and working fine.
After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !
I need the:
Alert.applicationIconBadgeNumber = x
to be updated in background each time the notification fires, I am repeating the notify by:
notif.repeatInterval = NSMinuteCalendarUnit
Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.
I am calling the scheduleNotification method by viewDidLoad
Here is my full code:
- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit;
NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:#"%i", BadgeNumber];
notif.applicationIconBadgeNumber = *BadgeNumberPointer;
notif.alertBody = BadgeNumberString;
notif.alertAction = #"Hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];
int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}
Kindly advice how to fix it,, thanking all of you.
Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.
Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.
I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.
I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in applicationIconBadgeNumber you can just use something like this:
notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:#"badgeCount"] + 1);
and then you could just reset it when the user responds accordingly.