How do I cancel a notification only? - iphone

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..

Related

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];
}

iOS notification override Silent mode - iPhone Alarm Clock App

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.

Cancel all notifications at specific time

For my App I don't want notifications to go off in the weekends.
So my idea was to cancel all the notifications at a specific time on Fridays, I would like to do this by scheduling the task, just like I schedule notifications (UILocalNotifications)
So for example I have this code for my alarms:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:8];
[comps setMinute:25];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *fireDate = [gregorian dateFromComponents:comps];
UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.fireDate = fireDate;
alarm.repeatInterval = NSDayCalendarUnit;
alarm.soundName = #"sound.aiff";
alarm.alertBody = #"Message..";
alarm.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
Is there a way to cancel all the notifications with the same method, or does Apple not allow this?
You can cancel a local notification with:
[[UIApplication sharedApplication] cancelLocalNotification:notification];
You would need to loop through all the local notifications and cancel them if they are on the weekend. Loop through them with:
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[notifications enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop){
if (/* notification.fireDate is on weekend */) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}];
However you will have to ensure the app runs on a Friday to perform the code to remove the weekend notifications.
But, why not just not schedule ones that are on the weekend in the first place?
You can cancel all the local notifications with the method
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you want to go through them, and maybe post the most important notifications, you could use scheduledLocalNotifications.

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.

scheduled local notification is not being stored in the scheduledLocalNotification array

I am currently scheduling local notifications to appear once per day at 6PM if a user has not already opened the app that day. If the user has already loaded the application, then I want to cancel the notification for that day and schedule a new one for tomorrow at 6PM. The notification displays properly, however, when I try to iterate of the list of scheduled notifications (this is not the only local notification I have in the application), the [[UIApplication sharedApplication] scheduledLocalNotifications] array is always empty. Below is the code snippet that is giving me trouble:
// See if we need to create a local notification to tell the user that they can receive a daily reward
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty
// Iterate over all the pending notifications
for( int iter = 0; iter < [notifications count]; iter++ )
{
UILocalNotification* localNotification = [notifications objectAtIndex:iter];
if( [[localNotification.userInfo objectForKey:#"source"] isEqualToString:#"dailyReminder"] )
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *nowComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:serverDate];
int hour = [nowComponents hour];
int minute = [nowComponents minute];
int second = [nowComponents second];
int hoursToAdd = 18 - hour;
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setHour:hoursToAdd];
[comps setMinute:-minute];
[comps setSecond:-second];
NSDate *tomorrow6PM = [calendar dateByAddingComponents:comps
toDate:serverDate
options:0];
NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:#"dailyReminder" forKey:#"source"];
scheduleNotification(tomorrow6PM, NSLocalizedString(#"Come Back!", #"daily reminder notification message"), NSLocalizedString(#"Launch", #"daily reminder notification button text"), [UIApplication sharedApplication].applicationIconBadgeNumber+1, userInfo);
the scheduleNotification function is straightforward and just constructs a local notification:
void scheduleNotification(NSDate* fireIn, NSString* bodyText, NSString* alertText, NSUInteger badgeCount, NSMutableDictionary* userInfo)
{
static int appCount = 0;
appCount += 1;
if(!isLocalNotificationEnabled())
return;
Class localNotificationClass = NSClassFromString(#"UILocalNotification");
UILocalNotification* localNotification = [[localNotificationClass alloc] init];
if(!localNotification)
return;
localNotification.fireDate = fireIn;
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertBody = bodyText;
localNotification.alertAction = alertText;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = badgeCount;
localNotification.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
I don't understand why the array of local notifications is always empty. Like I said before the notifications display at the correct time, so they are getting scheduled. Any help is appreciated.
Having similar issues right now. My guess here is that iOS does not schedule the notifications immediately but only at the end of the current run loop. I am running into these problems when setting the scheduledLocalNotifications property several times in the same run loop and changes don't seem to be updated accordingly. I think I will just keep a copy of the local notifications array myself and only set scheduledLocalNotifications and never read it.
localNotification.fireDate = fireIn;
in this line check wheather "fireIn" is object of NSDate or NSString.
If it's NSString convert it into NSDate object with help of NSDateformatter and then assing it to localNotification fireDate.
I was facing same problem previously and resolved it with above mentioned steps.
if the schedule has invalid fire date, it will not get scheduled and result empty array returned, because there never has a valid one schedule, try to print out your localNotification like below, you may find out the problem.
NSLog(#"Notification--->: %#", localNotification);