How to save the local notification? I want to use the local notification fire date again to schedule local notification.
You should use UILocalNotification
Please check out documentation: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/
It look something like:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"Alert Fired at %#", dateTime];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
About notification re-schedule cycle:
Firing the notification is not enough to show up another one. Your app won't be launch except if user tap the notification.
You could pre-schedule a notification for every day or week for the next month and unschedule them if user open one. Depending of your need you could also fire a remote notification with a content-available key which will launch your app in background and allow you to reschedule a new local notification.
Related
When I create my local notification callback didReceiveLocalNotificationgets triggered. The same callback gets triggered when I click on the local notification. Currently I was dividing those two cases by checking
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
//this means notification is clicked
}
But the main problem here is that when you are in the foreground and you slide your notification menu, and then receive your local notification, this callback didReceiveLocalNotification gets called. And in this case my app goes into this if. Because of this, I can't really distinguish from clicking the notification and creating a local notification while app is in the inactive state. Any ideas on how can I fix this?
This is the code for scheduling a local notification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"aaaa";
localNotification.alertTitle = #"title";
localNotification.userInfo = myUserInfo;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
After calling this, I get didReceiveLocalNotification delegate triggered.
I had to work around the exact same issue in my app. I couldn't find an offical way in the API to tell the difference, but here is a workaround.
Pass the current date in the userInfo when the local notification is created:
localNotification.userInfo = ["alertDate": NSDate()]
And when you handle didReceiveLocalNotification, check against current date again, to make sure it didn't just fire just a moment ago:
if application.applicationState == .Inactive {
if let alertDate = notification.userInfo?["alertDate"] as? NSDate
where (NSDate()).timeIntervalSinceDate(alertDate) > 0.1 {
// this means notification was initiated by user
}
}
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = #"image inserted";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self dismissViewControllerAnimated:YES completion:nil];
I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
I am working on a Contact manager app in which i have to schedule birthday notifications of contacts that have birthday on same day or one day before the current date. Can i do it in background or when my app is not running.
Is it possible? If Yes then give some helpful links or blogs.
Thanks
You cannot register for local notifications when you are in background.
Use the below code to register for local notification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Your text";
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:120.0]; // your date
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
When your are in foreground, your application:didReceiveLocalNotification will be called.
When you are in background or your app is terminated, you will get the local notification in Notification panel of the device
I am working with UILocalNotification. My app will give user facility to set the reminder for any meeting and medicine in my application.
I am Able to schedule UINotification according to my Schedule date. And also When user will schedule past date notification my secound schedule notification is not fired. After secound others are firing perfect but secound is not firing.
Following is my flow for notification:
I am creating 3 schedule for meeting one of its time is past of my current time say for e.g. 2014-01-28 18:00. And my current device time is 2014-01-28 19:00. Now i am creating another schedule its time is i.e. 2014-01-28 19:10. And another schedule date and time is i.e. 2014-01-28 19:20.
With the above schedule date time reference my first notification which was of past date was fired after schedule create.
When my current time of device come to 2014-01-28 19:10. I was especting notification. but it wount fire.
And at 2014-01-28 19:20 my 3rd notification fires as regularly. this things happen for me every times when I add past date as a schedule.
Following is my schedule notification code which i used to schedule notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = launchImage;
localNotification.applicationIconBadgeNumber = 1;
localNotification.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Also Following is my idReceiveLocalNotification: method to hendle notification:
if([UIApplication sharedApplication].applicationIconBadgeNumber == 0) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}else{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Also when i get notification at that time i will call MkLocalNotificationScheduler class to display alert which code is as follows:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Reminder"
message:[thisNotification alertBody]
delegate:self
cancelButtonTitle:#"NO"
otherButtonTitles:#"YES",nil] autorelease];
alert.tag = 1;
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
Please help me how to work on to get all Schedule notification weather it is past date or date yet to be come.
Do not create UILocalNotification for time that is past. The behavior that you are seeing is typical of UILocalNotification. It fires past event right away. So in your code check if the time has past and if it is then don't create a UILocalNotification. (also its non-sense to create a notification of past time)
Only create new UILocalNotification for times that are in future of your current time.
I am developing iOS app with both Push notification and Local Notification, I know how to remove 1 and all notification from center,
using
[[UIApplication sharedApplication]cancelAllLocalNotifications];
But my problem is if i have scheduled some local notifications ,
and a push notification is arrived so in didReceiveRemoteNotification
I am write
[[UIApplication sharedApplication]cancelAllLocalNotifications]; for clear notification center,
but it is cleared all my LocalNotification also...
EDIT
if there are total 3 notifiction in NC i.e. 1 is come from local notification and two from push( from server) in this case how can i handle it,? i am tap on 1st notification( comes from server) in NC. in this case what should do, my app badge should be 2.
then what should i do?
The cancelAllLocalNotifications will only cancel the local notifications, its even in the name! Not the push notifications as you can read in the documentation:
Cancels the delivery of all scheduled local notifications.
Since push notifications are server side there is noting to cancel in your app. To remove the push notification from the notification center just set the applicationBadegNumber to 0.
There is an option ,each notification contains a dictionary inside it, so when you creates any local notification add any key in dictionary which specifies that this notification is for local notification.so you can check that if it is not my local notification then i will remove it.
-(void)scheduleLocalNotification{
[self cancelAlarm]; //clear any previous alarms
UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.alertBody = #"alert msg";
alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime];
alarm.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:#"localNotification" forKey:#"localNotification"];
alarm.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
-(void)cancelNotification{
for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){
NSDictionary *userInfo = notification.userInfo;
if (![self.key isEqualToString:[userInfo objectForKey:localNotification]]){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
The applicationbadgenumber is managed by you. Deleting scheduled notifications will not change that number. You must manage that yourself. I gave a presentation a while back that might help. http://www.youtube.com/watch?v=ixQqZWtn0pg Start watching at 11:50.