OpsMgr Alerts Push Notification to Android Devices

5 minute read

Update 07 April 2013: I’ve updated this script again to support iOS devices and removed the requirements for PowerShell version 3. The updated script can be found here here.

Background

Stefan Stranger has written a 2-part blog post on how to use Windows Phone push notification for OpsMgr alerts. Stefan’s posts can be found here:

Part 1

Part 2

I got so excited about this idea, but I’m a big fan for Android when comes to mobile devices. I am currently using a Samsung Galaxy Nexus phone and a Samsung Galaxy Tab 2 10.1 tablet – both of them are Android devices. So I’ve spent some time to see if I can do the same for Android devices.

It turned out, there is also an app for Android devices, called “Notify My AndroidSmile. And the API’s for both apps are the same.

Setup Instructions

It’s pretty much the same way to get this setup for Android devices. I’ll now go through the steps to configure push notification for Android devices (and also for Windows Phones):

  1. Install “Notify My Android” on Android devices via Google Play

  2. Sign up – either from android device or from https://notifiymyandroid.com

  3. Generate an API key from https://notifymyandroid.com, under “My Account”

image

Note:

Stefan mentioned the “Notify My Windows Phone” app costs $1.99. “Notify My Android” app is actually free from Google Play, HOWEVER, after you’ve signed up, your account is only a trail account. the limitation for the trail accounts is that you only get 5 push notifications per day. Premium account has removed this limit It costs one-off payment of $4.99 to upgrade to premium account. the upgrade payment can either be made via Google Play on your android devices or using PayPal via the website. I upgraded my account via Google Play (as I thought it’s easier than using PayPal).

As I have 2 Android devices, I don’t need to create multiple accounts or generate multiple API keys. Once I’ve logged in on both devices using my premium account, the push notifications get delivered to both devices at the same time.

  1. copy this script to a unique location on all OpsMgr management servers in the Notifications Resource Pool (by default, this resource pool contains all management servers). In my lab, I have copied the script to D:\Scripts\MobileDevices-Notification on all my management servers. Below is what my updated script look like:
#Requires -Version 3
Param($os,$apikey,$alertname,$alertsource,$alertseverity,$alertTimeRaised,$alertdescription)

# Enter the name of the application the notification originates from.
$application = "OM2012"

# Enter The event that occured. Depending on your application, this might be a summary, subject or a brief explanation.
$event = "OM2012 Alert"

# The full body of the notification.
$description = @"
AlertName: $alertname
Source: $alertsource
Severity: $alertseverity
TimeRaised: $alertTimeRaised
Description: $alertDescription
"@

#description maximum length is 10000, truncate it if it's over
If ($description.length -gt 10000)
{
  $description = $description.substring(0,10000)
}
#You can enable the write-eventlog for logging purposes.
#write-eventlog -LogName Application -source MSIInstaller -EventId 999 -entrytype Information -Message $description

# An optional value representing the priority of the notification.
$priority = "-2"

# Specifies the responsetype you want. You can currently choose between JSON or XML (default)
$type = "json"
$os = $os.tolower()
Switch ($os)
{
  "android" {$uri = "http://notifymyandroid.com/publicapi/notify?event=$event&priority=$priority&application=$application&description=$description&apikey=$apikey&type=$type"}
  "windows" {$uri = "http://notifymywindowsphone.com/publicapi/notify?event=$event&priority=$priority&application=$application&description=$description&apikey=$apikey&type=$type"}
}

Invoke-WebRequest -Uri $uri

I’ve modified Stefan’s script (used in OpsMgr command notification channel) a little bit. Below is a list of what’s changed in my version of the script:

  • supports both Windows Phone’s “Notify My Windows Phone” app and Android’s “Notify My Android” app.
  • Script is more generic as the API key is not hardcoded in the script, instead, it’s passed into the the script as a parameter.
  • Push notification messages contain additional alert information – alert source, alert description. – As both app’s API’s supports maximum 10,000 characters in notification description, the script will truncate the message to 10,000 characters if it’s over the limit.
  • Additional parameters are required to be passed into the script. – therefore the OpsMgr command channel is different than Stefan’s version.
  1. Setup command notification channel:

Full path of the command line:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

Command line parameters:

D:\Scripts\MobileDevices-Notification\MobileDevicesPushNotifications.ps1 '<mobile device type>' '<API Key>' '$Data/Context/DataItem/AlertName$' '$Data[Default='Not Present']/Context/DataItem/ManagedEntityPath$\$Data[Default='Not Present']/Context/DataItem/ManagedEntityDisplayName$' '$Data/Context/DataItem/Severity$' '$Data/Context/DataItem/TimeRaisedLocal$' '$Data[Default='Not Present']/Context/DataItem/AlertDescription$'

Startup Folder:

D:\Scripts\MobileDevices-Notification

Note:

The script takes the following parameters (in the correct order):

  1. OS: support either “android” or “windows”

  2. API key: generated from either Notify My Windows Phone or Notify My Android website

  3. AlertName

  4. AlertSource: Addition to Stefan’s script.

  5. AlertSeverity

  6. AlertTimeRaised

  7. Alert Description: Addition to Stefan’s script.

The command line parameter can be populated by using pre-defined OpsMgr variables:

image

  1. Follow Part 2 of Stefan’s original post to setup Subscriber and Subscriptions.

I cannot test my version of the script against Windows phones because I don’t have one. But I’m fairly confident it should work. From what I can see, the API’s for both apps are exactly the same. Well, the only difference is the URL’s for API calls:

image

I think in real world, this is a very cost effective solution for alerts notification to mobile devices. According to the API documentations, multiple API keys can be used in a single API call. When using multiple API keys, the API keys needs to be separated by comma. Again, I have not tested this scenario against my script because I don’t really want to spend another $4.99 for another premium account.

If it’s required to setup push notification to multiple people (different mobile devices types, different notify app accounts and different Microsoft / Google accounts, thus different API keys), separate command notification channels need to be created (one per API key or group of API keys).

Below are what I get on my Android devices:

On the phone:

Screenshot_2013-03-31-00-33-07

On the tablet:

Screenshot_2013-03-31-00-37-03

Limitations

According the the FAQ pages on both websites, there are some limitations that are worth mentioning:

Notify My Windows Phone:

Are there any limitations to the amount of messages I can send?

Yes - The Microsoft Push Notification Service throttles notifications at 500 per 24 hours per device. The messages will however be stored temporarily at NotifyMyWindowsPhone, so they can be retrieved manually by hitting refresh from within the Windows Phone App.

Notify My Android:

Is there a rate limit for using the public API?

Yes. Right now the limit is set to 800 API calls per hour from a given IP. Remember that you can notify multiple API keys on a single API call. Please check the API documentation for more details. Very few applications managed to reach that limit so far, but if you hit the cap too often, feel free to contact us and ask for a developer key.

In my opinion, if you get over 500 messages a day on your Windows phone or 800 in one hour on android devices, there are something seriously wrong with your infrastructure :worried: or you might want to re-configure alert subscriptions to only notify really critical alerts.

Credit

Big thanks to Stefan for providing such an wonderful solution in the first place. :+1:

Leave a comment