Programmatically Create Reminder For NSUserActivity - ios

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.

Related

What is UILocalNotification repeatCalendar used for and how is it used

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.

UILocalNotification with repeat-once behaviour like in Messages App

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

How to post a local notification prior to 5 minutes before the actual time of the remainder in iOS program?

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

Local notification in iOS coming without any sound

-(void)notifyMe
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Alert";
localNotification.alertAction = #"Local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = nil;
localNotification.userInfo = nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
This is the method I am calling for local notification. This is working fine with other applications. But with my working one, notification is coming but with no sound.
I guess there is some problem with app setting, but I am unable to find it out.
I found your question as I had run into the same problem. Turns out it was a simple fix on my part, my code was fine all I had to do was enable push notifications for my app (which I had already done but was getting no sound or badge), and then switch Badge App Icon and Sound to ON.
I'm surprised there have been no other answers to this question in 6 months.

Perform Action At DatePicker Time

I need to send a text message when the current time equals the time selected in the UIDatePicker. How might I do this? You don't need to include the code to send the message, I already have that coded. I've tried all sorts of things with NSTimer and if - then statements but none have worked.
Edit: Since I wrote this question I've found a better way to do things. I just need to set a local notification and when received execute my code with -(void)didRevieveLocalNotification. Here is what I have so that any googlers can hopefully be helped.
NSDate *pickerDate = [self.datePicker date];
//Set Local Notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = pickerDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
//----------------------------------------------------------------------
notif.alertBody = #"Tap to send your text message!";
notif.alertAction = #"send message...";
notif.soundName = #"sms_alert_nova.caf";
notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
well i would use a local notification... something like this
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = theDate //The date that your picker has selected
notification.alertBody = #"Hey, the time just expired!"
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Then in your AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//Code to manage the notification logic
}
Hope this helps, the user will get the alert even if on background.. if on background the user must click the alert to let your application know that the local notification triggered, if he does (or he is on your app already, then the app delegate method will trigger letting your app know that the notification fired...
Hope this helps!

Resources