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
Related
i want to show local notification on every saturday like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
NSLog(#"day: %#", dayString);
if([dayString isEqualToString:#"Saturday"])
{
NSLog(#"Success");
[self PushNotification];
}
-(void)PushNotification
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = #"Test";
localNotification.alertBody =#"test of notification";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
[componentsForFireDate setWeekday: 7]; //for fixing Saturday
[componentsForFireDate setHour: 17]; //for fixing 5PM hour
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;
}
but my local notification is display in every minit then how can i display notification on every Saturday of the week.
thanks.
Try to declare fireData property in localNotification and schedule your notification
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hope this help
I want to get a random string from an array and then display this everyday at 7am.
Here is the code in my AppDelegate.m
-(void)applicationDidEnterBackground:(UIApplication *)application
{
QuoteTableViewController *quoteVC;
int randomlyGeneratedNumber;
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
NSString *stringOfTheDay = [[NSString alloc] init];
stringOfTheDay = [quoteVC.arrayOfOurQuotes objectAtIndex:randomlyGeneratedNumber];
NSLog(#"%#",stringOfTheDay);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:stringOfTheDay];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
There is a error at this line when i run the code
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
There is an array in my other View Controller. I did import the view controller.
EDIT :
Array has strings inside but when imported into app delegate it becomes 0. How to fix this?
You need to implement 'arrayOfOurQuotes' as a property of your App delegate instead of the view controller. The view controller and therefore your array will not be around anymore when the view is gone - which is obviously the case if you have already entered the background.
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 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);
}
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.