Home > View Post

It’s time for CRON (Day 2)

The scheduler (or CRON) is one of the features I've been most excited about us bringing to Mobile Services for some time. The need to execute code on a scheduled basis is such a common requirement that it feels great to bring the Z-factor experience to this problem.

image

Creating a new CRON job is super-easy. Inside your mobile service click on the SCHEDULER tab and click the CREATE button at the bottom of your screen. Setting up a schedule is so stupidly easy that I won't explain the intimate details here. However, note that you can setup a job without a schedule. This is handy if you want to start scripting before you know the schedule or, perhaps even more interestingly, you want to create a script that you can run "on demand".

This makes for a great place to keep a cleanup script, particularly handy for a demo (although you should wait until we cover the CLI in this series for the most bang there). One of my favourite uses for the scheduler's Run Once feature is the ability to quickly test and play with push notifications for your Windows 8, Windows Phone 8 or iOS device. Here's how to have some fun with push using run once.

First, you'll need to configure your application to receive push notifications - that's beyond the scope of this article but you can easily follow these tutorials to setup your Windows 8, Windows Phone or iOS app. Then you need to get a channel (or device token) for the push;

Windows 8

Once your app is all setup, quickly grabbing a channel URL for test purposes is a doddle

 var channel = PushNotificationChannelManager.
CreatePushNotificationChannelForApplicationAsync(); Debug.WriteLine(channel.Uri);

And then you can grab the channel from the output window with a simple cut and paste.

image

Head over to your new ‘on demand’ scheduled task and enter the following code (don’t forget to configure the service with the Package SID and client secret also).

push.wns.sendToastText04("https://channel-url-here", {
   text1: "Hello",
   text2: "This is a test",
   text3: "bananas"
});

And you should get your toast notification if everything is setup right.

Windows Phone:

Again, the flow is similar, but acquiring the channel is a little different:

var channel = HttpNotificationChannel.Find("MyPushChannel");

if (channel == null)
{
    channel = new HttpNotificationChannel("MyPushChannel");
    channel.Open();
    channel.BindToShellTile();
    channel.BindToShellToast();
}

Debug.WriteLine(channel.ChannelUri.AbsoluteUri);

And then to send the push:

push.mpns.sendToast("https://channel-url-here", {
    text1: item.title,
    text2: item.subtitle
});

iOS:

First, you’ll need to request regisration (in the AppDelegate.m):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
    // Register for remote notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
    UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge 
| UIRemoteNotificationTypeSound]; return YES; }

And subsequently handle the token and output to the log window:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken: (NSData *)deviceToken { NSLog(@"%@", deviceToken); }

You can then grab that token (without the angle brackets <>) and add to the following script

push.apns.send("your device token here", {
   alert: "Hello world";
});

Now you’re setup, hit that Run Once button. A great way to test push!

image

Oh and don't forget that you can actually schedule scripts too! Which makes scheduler great for:

  • periodically cleaning or archiving old data
  • aggregating data from a Mobile Services table or an external source ready for a report
  • poll another service and notify your users about events, e.g. stock prices or twitter activity
  • use CRON to roll multiple push notifications together to avoid interrupting users too often

Let me know in the comments or on twitter if you have any cool scenarios for the scheduler.

 
Josh Post By Josh Twist
8:01 PM
22 Dec 2012

» Next Post: Filter this (Day 3)
« Previous Post: Delete records and truncate tables (Day 1)

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.