Schedule a local notification for a specific time in Swift 2 - xcode

I've been all over these forums and other sites and I keep getting pieces of an answer that don't add up. Essentially, I would like to create a notification that fires, for example, every weekday at 6:28 AM, 12:28 PM, and 5:28 PM.
I have pieces of a solution, but I'm really unsure where to go. Am I setting this up right at all? Any help is appreciated.
let notification: UILocalNotification = UILocalNotification()
notification.category = "News and Sports"
notification.alertAction = "get caught up with the world"
notification.alertBody = "LIVE news and sports on VIC in just a minute!"
UIApplication.sharedApplication().scheduleLocalNotification(notification)

Preparing to show local notifications requires 2 main steps:
Step 1
On iOS 8+ your app must ask and, subsequently, be granted permission by the user to display local notifications. Asking permission can be done as follows in your AppDelegate.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if #available(iOS 8, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
}
return true
}
Do not call registerUserNotificationSettings(_:) when your app is running on a pre-iOS 8 operating system. Otherwise, your app will crash at runtime. Luckily, this shouldn't be a problem since you're working with Swift 2.
Step 2
Schedule your notification at a future fireDate.
let notification:UILocalNotification = UILocalNotification()
...
... // set the notification's category, alertAction, alertBody, etc.
...
notification.fireDate = ... // set to a future date
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Unfortunately, according to this Stack Overflow answer by #progrmr,
You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but
only limited options are provided. The repeatInterval parameter
is an enum type and it limited to specific values.
You cannot multiply those enumerations and get multiples of those
intervals. You cannot have more than 64 local notifications set in
your app. You cannot reschedule a notification once it fires unless
the user chooses to run your app when the notification fires (they may
not run it).
There is a request for repeat interval multipliers posted here.
You can add comments to it. I suggest filing a bug report or feature
request (url?) with Apple.
Many other Stack Overflow answers confirm the claims in the quotes above. Visit the link to the full quoted answer, which contains a list of supporting answers.
A potential workaround for your case would be to schedule 3 local notifications. Set each one to fire at 6:28 AM, 12:28 PM, and 5:28 PM, respectively. Then, set the repeatInterval of all 3 local notifications to .CalendarUnitWeekday.

Related

Trigger indefinite notification on Windows 10

I'm trying to trigger a notification which has no expiry but must be closed by pressing the top-right X close button. Is this possible?
I've been able to trigger a timed notification which also closes when anywhere else is clicked. With this answer.
[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"New Chat!","You have received New Chat!",[system.windows.forms.tooltipicon]::None)
Per Microsofts NotifyIcon.ShowBalloonTip Method documentation, the actual timeout property is set by the current system settings.
Minimum and maximum timeout values are enforced by the operating system and are typically 10 and 30 seconds, respectively, however this can vary depending on the operating system. Timeout values that are too large or too small are adjusted to the appropriate minimum or maximum value. In addition, if the user does not appear to be using the computer (no keyboard or mouse events are occurring) then the system does not count this time towards the timeout.
According to a couple of more google searches, you can set the time for your profile through the Registry ( Regedit - HKEY_CURRENT_USER\Control Panel\Accessibility: MessageDuration - didn't work for me).
Through group policy, or using theSystemParametersInfo API which is out of my league to explain any further. Only reference I can find was configuring the Accessibility/System Parameter: SPI_SETMESSAGEDURATION.
Its C++ though and only other article I could find was this one:SystemParametersInfoA function.
Seems possible but, it will definitely be a hassle to get it working.

Google Calendar v3 Error "The requested minimum modification time lies too far in the past. [410]"

We are using the Google Calendar v3 API to return a list of events for a user that have been updated since a point in time.
In the v2 API there was no limitation on setting this date in the past.
If we set the UpdatedMin to a date too far back (like 2 months) then the error is thrown
"The requested minimum modification time lies too far in the past. [410]"
If we set ShowDeleted to false then we do not get the error.
I cannot find any reference to a limitation here. Does anybody know the details of this limit. Unfortunately when synchronising calendars this is a show stopper when synchronisation has not run for a period of time for a calendar (other than running a full list which we would prefer to avoid)
EventsResource.ListRequest lr = new EventsResource.ListRequest(service, c.uc.calendar);
lr.UpdatedMin = c.primaryModTime.ToLocalTime();
lr.ShowDeleted = true;
Events el = lr.Execute();
if (el.Items.Count > 0)
{
the following also discusses this issue but without any resoluton.
https://groups.google.com/forum/#!msg/google-calendar-api/_rk9o45sXT0/3APXqxi8jvkJ
There is some explanation at:
https://developers.google.com/google-apps/calendar/v3/sync
It says that on 410 you should wipe your storage and perform a full sync instead.
Also consider switching to sync tokens as recommended in the last paragraph.

Glympse API - Prefil the Name of the Receivers in the Send Wizard

As there is a way to preset the Duration, Destination, Invitee Message, So Can we preset the Recipients of the Glympse too ?
One more thing I would like to ask is that I want to make Time Duration Wheel as Non Editable for that I am using following configuration:
final int WIZARD_FLAGS
= LC.SEND_WIZARD_INVITES_EDITABLE
| LC.SEND_WIZARD_MESSAGE_READONLY
| LC.SEND_WIZARD_DESTINATION_READONLY
| LC.SEND_WIZARD_TIME_READONLY;
// Launches the wizard which will send the Glympse
glympse.sendTicket(ticket, WIZARD_FLAGS);
But the Time Duration Field is still showing as EDITABLE, I just donot want to make it EDITABLE
Please tell me
The way to preset a recipient is to add an invite to the ticket before calling sendTicket like this:
_activeTicket = LiteFactory.createTicket(DURATION, MESSAGE, DESTINATION);
_activeTicket.addInvite(GC.INVITE_TYPE_SMS, "My friend", "555-555-5555");
// Launch the wizard with these pre-populated values and settings
GlympseLiteWrapper.instance().getGlympse().sendTicket(_activeTicket, 0);
As for setting the timer as read only, you have the correct flag set, but I see a bug that causes that flag to not work as intended. Currently the wheel can be edited even though it has no effect on the actual duration of the ticket (notice the time in the center goes up but the expire time above the wheel stays the same).
We'll make sure this is fixed in the next SDK release. For now even though it looks like the duration can be changed, the ticket that is sent will be the duration that you specify as a preset.

Any body have any luck with ShellTileSchedule?

Any body have any luck with ShellTileSchedule? I have followed the Microsoft example and still have gotten no where.
"How to: Update Your Tile Without Push Notifications for Windows Phone"
Has any one seen a complete example that works on a device or emulator?
Yes...I started with the sample at http://channel9.msdn.com/learn/courses/WP7TrainingKit/WP7Silverlight/UsingPushNotificationsLab/Exercise-2-Introduction-to-the-Toast-and-Tile-Notifications-for-Alerts/
and skipped immediately down to "Task 3 – Processing Scheduled Tile Notifications on the Phone." After that I had to wait about 1 hour, leaving the emulator running on my desktop (1 hour is the minimum update interval, indicated as such for "performance considerations."
_shellTileSchedule = new ShellTileSchedule
{
Recurrence = UpdateRecurrence.Interval,
Interval = UpdateInterval.EveryHour,
StartTime = DateTime.Now - TimeSpan.FromMinutes(59),
RemoteImageUri = new Uri(#"http://cdn3.afterdawn.fi/news/small/windows-phone-7-series.png")
};
Note that setting the StartTime to DateTime.Now - 59 minutes did nothing. It still waited a full hour for its first update. I could not find any mechanism to perform "go to this URI and Update yourself NOW!", other than calling out to a web service that tickles a Tile Notification.
as #avidgator said, you'll have to wait an hour.
i have written a tutorial on how to update the tile instantly here:
http://www.diaryofaninja.com/blog/2011/04/03/windows-phone-7-live-tile-schedules-ndash-executing-instant-live-tile-updates
basically it involves opening a push/toast update channel and then getting the phone to send "itself" a live tile update request. this will trigger the phone to go and get the tile "right now"
hope this helps
Are the channels necessary for this kind of update?
Is there a full code example of what has to be done to create an app that just updates its tile?
BTW: How about setting the Recurrence to UpdateRecurrence.Onetime and the StartTime to Now + 20 seconds for testing purposes?
I just got an tile update after an hour without channels and so on. So that answered my first question. But having to wait an hour while trying to develop an app is... unsatisfying.
It is easy. Just use the following code when you setup ShellTileSchedule.
ShellTile applicationTile = ShellTile.ActiveTiles.First();
applicationTile.Update(
new StandardTileData {
BackgroundImage = new Uri("www.ash.com/logo.jpg"),
Title = ""
});

Can i get a notification, when a new day begins?

In Cocoa, is there a notification i can register for, that informs me, when a new day begins - at 00h:00min:01s in the morning?
If it is for iPhone development, you can also listen for a UIApplicationSignificantTimeChangeNotification. It gets posted on more occasions than the arrival of midnight, but when you receive one, you can simply check if you are on or near midnight.
For Mac OS X, you would have to do what Tom Dalling suggests but you should also keep track of changes to the system clock yourself (in order to update your timer) as well as changes to the current time zone.
There's no notification that I know of. You can get a timer to fire whenever a new day begins like so:
[[NSTimer alloc] initWithFireDate:midnight
interval:60 * 60 * 24 //one day, in seconds
target:someObj
selector:#selector(someSelector)
userInfo:nil
repeats:YES];
The trick is getting an NSDate set to midnight. Check the Date and Time Programming Guide for how to do that with date components and the like.
EDIT: see this question for how to get the midnight NSDate.
As of iOS8, you can also directly listen to NSCalendarDayChangedNotification.
As mentioned by others, in iOS 8 you can use NSCalendarDayChangedNotification. In terms of how to do that, this post gives you more info.
Essentially NSCalendarDayChangedNotification requires that you go to your appDelegate file and insert the below code in app (adapted from guide for Swift 3):
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.dayChangedOperations(notif:)), name:NSNotification.Name.NSCalendarDayChanged, object:nil)
Where "ViewController" is the classname of one of my classes and "dayChangedOperations" is the name of the function that I want to run whenever the day changes.
Count Days Since Last Change
Note that all you get from this is an alert that the day changed. You are not given the number of days since the app last ran. So remember to save the date to a userDefault each time this function runs, so you can use it for comparison later.

Resources