Thanks to the great work down at the Novell labs, we (the poor C# programmers) can now at last put our hands on iPhone development using Monotouch. One of the most compelling features is the Apple Push Notification framework, where you can instantly display an alert message on the iPhone, put a number on that small red circle over your app icon, or even make a sound.
You currently have 2 options if you want notifications in your iPhone: a) implement your own C# client (consult the excellent apns-sharp project at http://code.google.com/p/apns-sharp/) which should consist of two applications, preferrably windows services, one for sending the notifications using a TCP client connected at Apple Push Notification servers and one for receiving feedback whenever a user uninstalls your application so you stop sending any more. b) save a lot of trouble and use the industry-grade urban airship service (http://www.urbanairship.com) which handles both tasks for you, and comes with one million free notifications per month (that's 1.000.000, yes, I know...)
If you get a free Urban Airship account, you can trigger a Push Notification with a simple API call. The following code should show you how. It assumes you have 3 entries in your application settings config file, one for notificationsKey, one for notificationsSecret and one for notificationsMasterSecret, you can find all these values in your application's settings page. Depending on the API call, sometimes you need the secret (e.g. device registration) and sometimes you need the master secret (e.g. push call).
private const string URBAN_SHIP_URL = "https://go.urbanairship.com/api/";
private void SendRequest(string command,string json,bool useMasterKey)
{
WebClient c = new WebClient();
c.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["notificationsKey"], (useMasterKey ? ConfigurationSettings.AppSettings["notificationsMasterSecret"] : ConfigurationSettings.AppSettings["notificationsSecret"]));
c.Headers.Add("Content-type", "application/json");
byte[] data = Encoding.UTF8.GetBytes(json);
byte[] ret = c.UploadData(URBAN_SHIP_URL + command + "?format=xml",data);
}
public void Push(string deviceToken, string message)
{
string json = string.Format("{{\"device_tokens\": [\"{0}\"],\"aps\": {{\"alert\": \"{1}\"}}}}",deviceToken,message);
SendRequest("push/",json,true);
}
public void Push(string deviceToken, int badge)
{
string json = string.Format("{{\"device_tokens\": [\"{0}\"],\"aps\": {{\"badge\": {1}}}}}", deviceToken, badge.ToString());
SendRequest("push/", json, true);
}
There is one method for sending alerts and one method for changing the badge, I have not included a method for making a sound on the iPhone but you should get the idea. All you have to do is call Push("xxxxxxxxxxxxxxxxxx","Hello World!"), and if you have correctly setup and installed the application on the device, it should display an alert even if the application is not running! Cool huh?
It is a bit tricky to setup eveything correctly on the Monotouch side, so I suggest you take a look at http://code.google.com/p/apns-sharp/downloads/detail?name=MonoTouch-ClientPushNotificationSample-v2.zip, all the code you need is already written for you.
No comments:
Post a Comment