I have created a UILocalNotification I would like to know how ever what repeateCalendar can be used for?
This is my simple implementation of a UILocalNotification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:dateFromString];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
// Setup alert notification
localNotification.alertTitle = [NSString stringWithFormat:#"Reminder (%#)", amPm];
localNotification.alertBody = [NSString stringWithFormat:#"This is my alertBody"];
localNotification.userInfo = identificationDict;
localNotification.repeatInterval = NSWeekCalendarUnit;
//localNotification.repeatCalendar = what do I put here? and why;
localNotification.soundName=UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
Currently Apple docs say
The calendar the system should refer to when it reschedules a
repeating notification
However no discussion on why you would use it? In theory could I use it to define Weekday and Weekends?
Other than the most widely used calendar - Gregorian calendar, there are a few other calendars, for example in China, we have a Chinese calendar (NSCalendarIdentifierChinese) which is totally different from the Gregorian calendar. We use that calendar for our traditional holidays and astronomical dates. So if the users are mainly Chinese, then you probably need to set repeatCalendar to Chinese calendar, otherwise your notification might fire on the wrong date. Here is the docs from Apple talking about all different kinds of calendars.
Related
With iOS 9, NSUserActivities can be used with Siri, for example, "Siri, remind me of this in an hour", creates the reminder with her knowing the context of what "THIS" is. Is there a way to programmatically do that, so at the click of a button you could create a reminder?
You could simply schedule a UILocalNotification that will remind the user:
NSDate *dateInOneHour = [[NSDate date] dateByAddingTimeInterval:3600];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = dateInOneHour;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Don't forget about THIS";
localNotif.alertAction = #"View";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Not an answer as such, but a path to one: since reminders a la the Reminders app are managed by EventKit (see https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html), try making a Smart Reminder using Siri, then examine the resulting reminder using the API. At a guess, it's using the -URL property on the reminder.
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've created a reminder in my app, I want the remainder to be notified before 5 minutes of its actual time. If yes, is there any default available for doing it?
Thanks!
For this you can add two reminders, one for actual time and other 5 minutes before the actual time. Now you can do different things on receiving both reminders..
you can remove 5 min by creating new date from your date by
NSDate *fiveMinutesBeforeDate = [NSDate dateWithTimeInterval:-60*5 sinceDate:dateFromFirstString];
and create local notification by following cate and set this date to firedate of local notification
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fiveMinutesBeforeDate;
localNotification.alertBody = [NSString stringWithFormat:#"Alert Fired at %#", fiveMinutesBeforeDate];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I have made one sample application which fires local notification.
When notification fires it always shows banner in notification area in device, which I have shown in image.
But I want alert rather than this and want to perform action based upon selected option from that alert.
Code to fire local notification is given as below.
-(IBAction)setNotification:(id)sender{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
{
return;
}
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Get the year, month, day from the date
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSTimeZoneCalendarUnit|NSSecondCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[NSDate date]];
// Set the second to be zero
components.minute = components.minute + 1;
components.second = 0;
// Create the date
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
NSLog(#"Fire Date :: %#",date);
localNotif.fireDate = date;
localNotif.alertBody = [NSString stringWithFormat:#"First Alarm"];
localNotif.alertAction =#"Ok";
localNotif.soundName=#"Alarm_1.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.alertAction = #"Application name";
localNotif.HasAction = true;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Can any body please tell me if there is any mistake.
Thanks in advance.
Here is a quick and short answer. You cannot do that.
Apples documentation only states didReceiveLocalNotification. The way the notification is shown is not up to the developer. The user will choose how he wants to see notification using SETTINGS.
In your case, just wire up logic by implementing the delegate callback when the user taps on the notification.
Change the Type of Notification in Setting -> Notification Centre - > Your App -> Alert:
Originally from Quinn "The Eskimo!", as quoted by IBG:
"This depends on you mean. You have some control over how the
notification appears based on how you set the UILocalNotification
properties (things like alertBody, soundName, and so on). However, if
you're asking about the way in which those properties are interpreted
(the things the user can customise in Settings > Notifications), those
are user preferences and not exposed via any API."
You can get your notification in this method:
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
write this code for fetch the data from userInfo:
[[userInfo objectForKey:#"aps"] objectForKey:#"price"]];
use userInfo Dict to get The notification Value and after that you can use that data for Alert.
iam getting some date and time like appointment time. i want to remind the user before to that particular time.i googled and got some knowledge but still in small confusion where to write my code shall i in application entered back ground method and i have to use alert views or any other .please help me.
NSString *today=[NSString stringWithFormat:#"%# %#",appDelegate.appointmentDate,appDelegate.timeStart1];
NSLog(#"%#",today);
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *date1=[format dateFromString:today];
NSLog(#"%#",date1);
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *datefo1 =[[NSDateComponents alloc]init];
[datefo1 setMinute:1];
[datefo1 setSecond:0];
NSDate *alerttime=[cal dateByAddingComponents:datefo1 toDate:date options:0];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = alerttime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.alertBody = #"you have an appointment in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}
i want notification like on top i want to display some alert even my app is running and not running also. but its coming with out any message, where i am going wrong shall i need to use nsnotification center are some thing else. sorry for my poor english.thanks in advance
You're using local notifications, which is a good start, but you should know that local notifications are only displayed if your app is in the background. If your app is running in the foreground when the time comes you will need to display the notification yourself (UIView / UIAlertView).
You can schedule the local notification whenever you want, it really depends what editing you allow the user to do and, therefore, how much you might need to cancel the local notification and schedule a new version.
Few other things:
Consider using 'NSDate -dateByAddingTimeInterval' (it's just less code for your use case)
You're adding a minute to your date, so the alarm will be after the event (and say the appointment is in 30 minutes time ?)
Use NSLocalizedString for any string that will be displayed to the user
In the future, store less data in the appDelegate, that ins't what that class is for :)