Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dannycabrera/9769419 to your computer and use it in GitHub Desktop.
Save dannycabrera/9769419 to your computer and use it in GitHub Desktop.
AppDelegate ReceivedRemoteNotification Example
public override void ReceivedRemoteNotification (UIApplication application, NSDictionary userInfo)
{
processNotification(userInfo, false);
}
void processNotification(NSDictionary options, bool fromFinishedLaunching)
{
//Check to see if the dictionary has the aps key. This is the notification payload you would have sent
if (null != options && options.ContainsKey(new NSString("aps")))
{
//Get the aps dictionary
NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string alertTitle = string.Empty;
string alert = string.Empty;
string sound = string.Empty;
int badge = -1;
//Extract the alert text
//NOTE: If you're using the simple alert by just specifying " aps:{alert:"alert msg here"} "
// this will work fine. But if you're using a complex alert with Localization keys, etc., your "alert" object from the aps dictionary
// will be another NSDictionary... Basically the json gets dumped right into a NSDictionary, so keep that in mind
if (aps.ContainsKey(new NSString("alert")))
alert = (aps[new NSString("alert")] as NSString).ToString();
if (options.ContainsKey(new NSString("alertTitle")))
alertTitle = (options[new NSString("alertTitle")] as NSString).ToString();
//Extract the sound string
if (aps.ContainsKey(new NSString("sound")))
sound = (aps[new NSString("sound")] as NSString).ToString();
//Extract the badge
if (aps.ContainsKey(new NSString("badge")))
{
string badgeStr = (aps[new NSString("badge")] as NSObject).ToString();
int.TryParse(badgeStr, out badge);
}
//If this came from the ReceivedRemoteNotification while the app was running,
// we of course need to manually process things like the sound, badge, and alert.
if (!fromFinishedLaunching)
{
//Manually set the badge in case this came from a remote notification sent while the app was open
//if (badge >= 0)
// Do something with the badge here
//Manually play the sound
if (!string.IsNullOrEmpty(sound))
{
//This assumes that in your json payload you sent the sound filename (like sound.caf)
// and that you've included it in your project directory as a Content Build type.
//var soundObj = MonoTouch.AudioToolbox.SystemSound..FromFile(sound);
//soundObj.PlaySystemSound();
}
//Manually show an alert
if (!string.IsNullOrEmpty(alert))
// Display alert message here
}
}
/*
//You can also get the custom key/value pairs you may have sent in your aps (outside of the aps payload in the json)
// This could be something like the ID of a new message that a user has seen, so you'd find the ID here and then skip displaying
// the usual screen that shows up when the app is started, and go right to viewing the message, or something like that.
if (options.ContainsKey(new NSString("customKeyHere")))
{
//You could do something with your customData that was passed in here
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment