i am trying to get notification when date of array become same as current date this code is i am using for this task
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"dd/MM/yyyy"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
for (int i=0;i<_convertedBdates.count;i++)
{
NSDate *date =[Form dateFromString:[_convertedBdates objectAtIndex:i ]];
NSLog(#"date%#",date);
if(notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"New ";
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
suggest me what i am doing wrong
have you put this code in your app delegate?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Notification Received, %#, set for date %#", notification.alertBody, notification.fireDate);
}
if problem persist then i am here :)
If your NSDate is less than the current date then notification will not be fire. If you are setting notification date on the user birthdate then this will happen.
If your application is in running mode then :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
above method is called.
Hope this will help you.
All the best !!!
Related
I am using local notifications in my app. It's firing ok, but when I open the app from the notification, I can't access notification's userinfo. This is how I am scheduling the notification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = #{#"eventId" : eventID};
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"];
NSDate *dateStart = [dateFormatter dateFromString: [eventDictionary valueForKey:#"time_start"]];
NSDate *newDate = [[NSDate date] dateByAddingTimeInterval:15];
notification.fireDate = newDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertAction = #"Open";
notification.alertBody = #"Some title";
notification.soundName = UILocalNotificationDefaultSoundName;
//cfilipi: todo: verify if event date is bigger than today
if ([newDate timeIntervalSinceNow] > 0) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
This is how I am handling when I open the app from the notification (AppDelegate.m):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] != nil){
//even when I cast to NSDictionary, it gets other class type (UIConcreteLocalNotification, I can't find any information about this class)
NSDictionary * aPush = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
[self handleLocalNotification:aPush];
}
}
-(void)handleLocalNotification:(NSDictionary *)localNotification{
NSDictionary *userInfo = [localNotification objectForKey:#"userinfo"];
NSString *eventId = [userInfo objectForKey:#"eventId"];
if ([eventId length] >= 1) {
//I use this key in some parts of my code
[[NSUserDefaults standardUserDefaults] setObject:eventId forKey:#"eventId"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
And this is what I get from LLDB when I am debugging didFinishLaunchingWithOptions:
(lldb) po aPush
<UIConcreteLocalNotification: 0x145b2ba0>{fire date = quinta-feira, 18 de dezembro de 2014 08:47:13 Horário de Verão de Brasília, time zone = America/Sao_Paulo (BRST) offset -7200 (Daylight), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = {
eventId = 21;
}}
My question is: what am I doing wrong? Is there any way to convert this UIConcreteLocalNotification to NSDictionary? I have tried to convert it to UILocalNotification too, but I had no success.
Regards!
Do like this and it will work fine:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil )
{
[self handleLocalNotification:[notification userInfo]];
}
}
You have not implemented
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Implement this method and try.
I have used the following code to send the local notification in particular time. It works fine when calling from a method but when i am calling from another method its not working.
Code as follows:
-(void)notificationUserInfo:(NSDictionary *)notificationData
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm"];
NSString *tipid = [notificationData objectForKey:#"receivedTipsId"];
NSString *currentDate = [notificationData objectForKey:#"receivedDate"];
NSString *tipsCategoryid = [notificationData objectForKey:#"categoryId"];
NSString *todayDateTime = [notificationData objectForKey:#"todayDate"];
NSString *categoryName = [notificationData objectForKey:#"categoryName"];
NSString *tipsForNotification = [notificationData objectForKey:#"tipsForNotification"];
NSString *cardType = [notificationData objectForKey:#"cardType"];
//Assigning the notification contents
NSDate *updatedDateFormat = [dateFormat dateFromString:todayDateTime];
notification.fireDate = updatedDateFormat;
notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:tipid, #"receivedTipsId", currentDate, #"receivedDate", tipsCategoryid, #"categoryId", cardType, #"cardType", nil];
notification.alertBody = [NSString stringWithFormat:#"%#: %#",categoryName, tipsForNotification];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Thanks in advance.
If you are using this in iOS 8 then you have to register your app for local notification.
IN app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*...*/ }
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
And then
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (!localNotification) {
break;
}
**localNotification.timeZone = [NSTimeZone defaultTimeZone];**
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:20]];
localNotification.alertBody = #"message";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Use above code.
This is because of problem with time and date format. When i used this method, I have given the firedate as past date. eg., instead of dd/MM/yyyy i used MM/dd/yyyy.
So 11/12/2014 is returned as 12/11/2014. So the notification is received immediately after the notification scheduled.
My app is about showing student timetable and I want to add a local notification repeatInterval.
I plan to use switch button for this.
I tried this code and it doesn't work right now :
-(IBAction)theSwitch:(id)sender{
if (switcher.on) {
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:3]; // For tuesday
[dateComponent setHour:13];
[dateComponent setMinute:41];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:#"ALARM!"];
[notification setFireDate:fireDate];
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
else {UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:nil];
}
}
So I need some more guidance here on this and some suggestions on how to achieve this.
If you not able to get local notification than please check this code below.
Add code in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
NSLog(#"Recieved Notification %#", localNotification);
}
return YES;
}
Add method to handle local notification
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#", notification);
}
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I'm not receiving local notification,can anyone say how to get local notification.
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
Try this!!!!
You need to specify the time when to fire notification. You have just created notification but not set when it need to be fired.
For example..
localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];
If application is foreground stage then you wan't seen those notification. you should have to code on delegate method.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
UILocalNotification *notif = [[UILocalNotification alloc]init];
notif.alertBody = #"Your Session is Ready";
notif.alertAction = #"Take Now";
notif.soundName = #"DingBell.wav";
[notif setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notif.repeatInterval = NSWeekCalendarUnit;
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableDictionary *userDict = [NSMutableDictionary dictionary];
[userDict setValue:self.stringModuleID forKey:#"kRemindMeNotificationDataKey"];
[userDict setValue:[NSString stringWithFormat:#"alarm%d",[[setreminingDay objectAtIndex:i] intValue]] forKey:#"alarm"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
I tried to re-write the following tutorial under Xcode 4.6.3 - http://vimeo.com/29824336
But I have the problem that at around 24:00 my code is not bringing up the alarm:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up!";
notification.soundName = #"SNSD-Oh.caf";
NSLog(#"Ring Ring Ring!");
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
}
- (IBAction) alarmSetButtonTapped:(id)sender
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
NSLog(#"Alarm Set Button tapped: %#", dateTimeString);
[self scheduleLocalNotificationWithDate: dateTimePicker.date];
}
What did I do wrong?
Your app must not be running in foreground when the notification fires.
If you want to play sound in foreground,you can use combination of UIAlertView and 'AvAudioPlayer'.
You can setup this code in
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification delegate method of AppDelegate.m.
Feel free to ask further.