iOS notification override Silent mode - iPhone Alarm Clock App - iphone

I am working on an alarm clock App
The alert + music channels through the notification centre
It will not activate if the iPhone is set to silent mode
Many users have requested that the alarm overrides silent mode
Is this possible?
Cheers!
(void)addLocalNotification:(myAlarmData *)ma WeekDay:(int)mweekday
{
UILocalNotification *noti = [[UILocalNotification alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *dcom = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:now];
[dcom setWeekday:mweekday];
[dcom setHour:ma.mHour];
[dcom setMinute:ma.mMinute];
[dcom setSecond:0];
NSDate *fireDate = [gregorian dateFromComponents:dcom];
noti.fireDate = fireDate;
noti.timeZone = [NSTimeZone localTimeZone];
noti.alertBody = [NSString stringWithFormat:#"Wake up %#!", [GlobalData gSettings].name];
noti.soundName = [NSString stringWithFormat:#"%#.caf", ma.soundName];
noti.alertAction = #"OK";
noti.repeatInterval = NSWeekCalendarUnit;
noti.userInfo = [[[NSDictionary alloc] initWithObjectsAndKeys:
ma.mid, #"mid",
[NSString stringWithFormat:#"%d", mweekday], #"day",
ma.soundName, #"sound",
[NSString stringWithFormat:#"%d", ma.snooze], #"snooze",
ma.title, #"title",
#"Close", #"action",
#"real", #"more",
nil] autorelease];
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
[noti release];
[gregorian release];
}

We can override silent switch to play sounds.
In this case it depends on how iOS manages to play sounds when a notification is triggered.
Have you checked this on device.?

You should take a look at AudioSession categories.
AVAudioSessionCategoryPlayback should do the trick.

Related

How do I cancel a notification only?

I wanted to do something. My notifications are set to 11 and 12 hours. I would like to unsubscribe notification of these are set just hours 11. How do I make it?
My code;
NSCalendar *greg = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * component = [greg components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[component setYear:2013];
[component setMonth:m];
[component setDay:d];
[component setHour:x];
[component setMinute:y];
UIDatePicker *dd = [[UIDatePicker alloc]init];
[dd setDate:[greg dateFromComponents:component]];
UILocalNotification * bildirim = [[UILocalNotification alloc]init];
[bildirim setAlertBody:newtask.name];
[bildirim setFireDate:dd.date];
bildirim.soundName = UILocalNotificationDefaultSoundName;
bildirim.alertAction = NSLocalizedString(#"Test", nil);
//[bildirim setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
[bildirim setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:bildirim];
I found a code. Unfortunately this code is deleting all notifications.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Sorry for my bad english.
Two things while you are creating local notification, create it with some unique identification so that you can identify like below
UILocalNotification * notification = [[UILocalNotification alloc]init];
notification.userInfo=[NSDictionary dictionaryWithObject:#"UNIQUE_ID" forKey:#"notifcation_id"];
Then to cancel the specific one you need the following steps
for(UILocalNotification *notificaiton in [[UIApplication sharedApplication] scheduledLocalNotifications]){
if([[notificaiton.userInfo objectForKey:#"notifcation_id"] isEqualToString:UNIQUE_ID]){
[[UIApplication sharedApplication] cancelLocalNotification:notificaiton];
break;
}
}
All the best..

Schedule local Notification after app was closed for 7 days

It was quite a bit hard to enable local notifications for my application.
And yeah, I set up my project with a local notification witch I want to schedule after the app had been closed for one week. For example if the user opens the app on saturday 8th, the local notification should appear on the next saturday the 15th, but the time should changed. For example he/she closes the app at 8pm and I don't want to disturb them at 8pm next week so I want to display every notification at 6pm or something like that.
Now you know my problem and here is my code I'm using:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
//set day (saturday)
[componentsForReferenceDate setDay:26] ;
[componentsForReferenceDate setMonth:1] ;
[componentsForReferenceDate setYear:2013] ;
[componentsForReferenceDate setHour: 17] ;
[componentsForReferenceDate setMinute:30] ;
[componentsForReferenceDate setSecond:00] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"Du wirst vermisst! \nDeine App braucht dich, schreibe sie zu Ende!"] ;
notification.alertAction = #"Zurückkehren";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"Some information"] forKey:#"information"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName = #"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
}
I know that there have to be more methods for deleting the badge and the didrecievenotification, but I let them out because there are not important for this.
With this code I managed to schedule the notification on every saturday at 5:30pm (Germany). But I wanted to schedule it only once, when the app had been closed for exactly one week. Is that somehow possible? I would be glad if someone could correct my code or give me a solution for this.
Best regards and thank you for reading this long post,
Noah
You should unschedule previous notification.
for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
if ([[lNotification.userInfo valueForKey:#"information"] isEqualToString:#"Some information"])
{
[[UIApplication sharedApplication]cancelLocalNotification:lNotification];
break;
}
}
Set fire date by below way:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:[NSDate date]];
[componentsForReferenceDate setHour: 17] ;
[componentsForReferenceDate setMinute:30] ;
[componentsForReferenceDate setSecond:00] ;
NSDate *tempDate = [calendar dateFromComponents: componentsForReferenceDate];
[componentsForReferenceDate release];
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setDay:7];
NSDate *fireDateOfNotification = [calendar dateByAddingComponents:comps
toDate:tempDate options:0]
[comps release];
The notification.repeatInterval = NSWeekCalendarUnit is causing this. use:
notification.repeatInterval = 0;
This prevent repeating (source).
Here is the Swift 2.0 adaptation
Unscheduling the current notification
for notifMe:AnyObject in UIApplication.sharedApplication().scheduledLocalNotifications!{
let title = notifMe.userInfo["information"] as? String
if title == "some information"{
UIApplication.sharedApplication().cancelLocalNotification(notifMe as! UILocalNotification)
}
}
Setting the date for the notification
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.Year.union(NSCalendarUnit.Month).union(NSCalendarUnit.Day),fromDate: date)
components.hour = 17
components.minute = 30
components.second = 00
let tempDate = calendar.dateFromComponents(components)!
let comps = NSDateComponents()
comps.day = 7
let fireDateOfNotification = calendar.dateByAddingComponents(comps, toDate: tempDate, options:[])

iPhone Alarm App mysterious inactivation (UILocalNotification)

I am in the final stages of development for an iPhone Alarm App
The alarm works through notification centre.
Work well for short time delays (say 2 hours)
Does not work in the morning (Long time delay 8 hours)
Also it seems to work fine on phones that do not have internet access?
Could there be some kind of automatic time updating/syncing that is disturbing alarm function?
Is the 8 hour+ time delayed neutralising the notificaiton?
Does the app need to be in the foreground?
So the alarm works for short time delays, but not in the morning (When it is meant to!)
We are due to submit update to Apple very soon!
Thank you Kindly,
Henry (Aus.)
-(void)addLocalNotification:(myAlarmData *)ma WeekDay:(int)mweekday
{
UILocalNotification *noti = [[UILocalNotification alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *dcom = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:now];
[dcom setWeekday:mweekday];
[dcom setHour:ma.mHour];
[dcom setMinute:ma.mMinute];
[dcom setSecond:0];
NSDate *fireDate = [gregorian dateFromComponents:dcom];
noti.fireDate = fireDate;
noti.timeZone = [NSTimeZone localTimeZone];
noti.alertBody = [NSString stringWithFormat:#"Wake up %#!", [GlobalData gSettings].name];
noti.soundName = [NSString stringWithFormat:#"%#.caf", ma.soundName];
noti.alertAction = #"OK";
noti.repeatInterval = NSWeekCalendarUnit;
noti.userInfo = [[[NSDictionary alloc] initWithObjectsAndKeys:
ma.mid, #"mid",
[NSString stringWithFormat:#"%d", mweekday], #"day",
ma.soundName, #"sound",
[NSString stringWithFormat:#"%d", ma.snooze], #"snooze",
ma.title, #"title",
#"Close", #"action",
#"real", #"more",
nil] autorelease];
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
[noti release];
[gregorian release];
}

Only let local notification appear once

I have a huge problem! My problem is, that I've implementet a localnotification code in my delegate. I've put in this code in didEnterBackround method. However, I get a localnotifi for each didEnterBackround. Is there a possible way that only a new notification firedate starts, if the latest was gone? Like if I have a localnotifi which fires after 5 days. When I open and close the app 5 times and wait 5 days I get 5 localnotifications. Is it possible to let the app make only one firedate and after this appears a new one gets startet?!
My code:
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate];
NSDateComponents *timeComponents = [calender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:currentDate];
NSDateComponents *temp = [[NSDateComponents alloc]init];
[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour:[timeComponents hour]];
[temp setMinute:[timeComponents minute]];
[temp setSecond:[timeComponents second] +10];
NSDate *fireTime = [calender dateFromComponents:temp];
UILocalNotification *localN = [[UILocalNotification alloc]init];
localN.fireDate = fireTime;
localN.timeZone = [NSTimeZone defaultTimeZone];
localN.alertBody = #"BLA BLA BLA!";
localN.alertAction = #"GO BACK";
localN.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localN];
You're able to cancel all scheduled notifications:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you don't want to remove ALL scheduled notifications (perhaps you have something else scheduled), then take a look at cancelLocalNotification: which allows you to cancel only a single instance.

LocalNotifiction for wakeup application

In my application i want to set local notifiaction on a particular time if my application killed from background else if my application in running condition then i dan't want to show localnotification
I have written code for local notification
NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate1 =self.dateEvening;//[self.dateEvening dateByAddingTimeInterval:60*1];// [self.pickerView date];
// Break the date up into components
NSDateComponents *dateComponents1 = [calendar1 components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *timeComponents1 = [calendar1 components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *dateComps1 = [[NSDateComponents alloc] init];
[dateComps1 setDay:[dateComponents1 day]];
[dateComps1 setMonth:[dateComponents1 month]];
[dateComps1 setYear:[dateComponents1 year]];
[dateComps1 setHour:[timeComponents1 hour]];
[dateComps1 setMinute:[timeComponents1 minute]];
[dateComps1 setSecond:[timeComponents1 second]];
NSDate *itemDate1 = [calendar1 dateFromComponents:dateComps1];
[dateComps1 release];
UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
if (localNotif1 == nil)
return;
localNotif1.fireDate = itemDate1;
localNotif1.timeZone = [NSTimeZone defaultTimeZone];
localNotif1.alertBody = #"Open application";
localNotif1.alertAction = #"View";
localNotif1.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
[localNotif1 release];
Above code works fine if application will close from background but if my application in running condition then I don't want local notifications to be fired.