Windows Phone Push notification

Push Notification helps you in sending event or alert to your app users even when they are not logged into the app. App42 uses MPNS (Microsoft Push Notification Service) to send Push Notification on Windows Phone. App42 currently supports Toast and Tile notifications for Windows Phone. You can send push notifications from UI console as well as can use APIs to send and schedule Push notification to your app users. App42 also gives you interesting insight about analytics of push messages too. Here are the few easy steps to get started with App42 Push Notification using a sample project in a few minutes.

1. Start building
  • Register with App42 platform
  • Create an app once you are on Quick-start page after registration.
  • If you are already registered, login to AppHQ console and create an app from App Manager -> App Create link.
  • Download sample project from our GitHub Repo
  • Unzip the downloaded file and open it in Microsoft Visual Studio Express for Windows Phone.
  • Open MainPage.xaml.cs file and put your API KEY and SECRET KEY which you have received after the success of app creation from AppHQ Console in the code Snippet which has been shown below and also put value of userId which you want to register for push notifications.
ServiceAPI sp = new ServiceAPI("<YOUR API KEY>","<YOUR SECRET KEY>");
String userId = "<Your App User>";

If you want to go into detail about the sample code, please see MainPage.xaml.cs which does all MPNS related work like registering with MPNS and App42 server and generating notifications. Once your app gets registered with MPNS, its PushChannel_ChannelUriUpdated method gets invoked.

For Toast Notifications we use “BindToShellToast” and for Tile Notification we use “BindToShellTile” method on channel to register channel to receive notifications.

public MainPage()
{
	// Build Push Notification Service   
	PushNotificationService pushObj = sp.BuildPushNotificationService();
	channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
	channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
	channel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

	channel.Open();
	Collection<Uri> TileLocations = new Collection<Uri>();
	//Domain of image file uploaded for tile and background image. 
	//If image is on App42 server, it should be cdn.shephertz.com
	TileLocations.Add(new Uri("http://cdn.shephertz.com/"));
	channel.BindToShellTile(TileLocations);
	channel.BindToShellToast(); 
}
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
  StoreURIWithApp42(e.ChannelUri.ToString());
}
void StoreURIWithApp42(String ChannelUri) 
{
  pushObj.StoreDeviceToken(userName, ChannelUri, this); 
}
void App42Callback.OnException(App42Exception exception)
{
  // here Exception is handled
  Console.WriteLine(exception.ToString());
}
void App42Callback.OnSuccess(object response)
{
  // here success is shown
  Console.WriteLine(response.ToString());
}

The Above snippet will do all the stuff for you seamlessly. Once app will get started it will register your app with MPNS as well as with App42 platform for push notification usage. It uses StoreDeviceToken method to register app with App42 server.This is only one time activity and will be done when app will be opened.

  • Build your project and run.
3. Sending Toast Message to User

Once your app is registered you are ready to send message to your app user using following code snippet from any App42 SDK. Below is the snippet for App42 Windows Phone SDK. If you want to send it from other SDK you can do it in the same way.

private void SendTost(object sender, RoutedEventArgs e)
{
	String userName = userId; //Defined in MainPage.xaml.cs
	String message = "Hi...";
	String subTitle = "MyAppMessge";
	String param = "/MainPage.xaml";
	pushObj.SendPushToastMessageToUser(userId, message, subTitle, param , callback);
}
4. Sending Tile Message to User

From API

private void SendTile(object sender, RoutedEventArgs e)
{
	Tile tileData = new Tile();
	//Image URL of live tile image. Not greater than 80KB as per MPNS specs.
	tileData.SetBackgroundImage("http://cdn.shephertz.com/repository/files/760a0c04733f8d19bbec63cc5caebd585466ab0c2e63eed9219d601266a20eb9/c7192c8bf15aa01e88f599252bdb7c0a8ef531c1/TileImage.png");
	tileData.SetCount("2");
	tileData.SetTitle("What's up");
	tileData.SetBackContent("Notification");
	//Image URL of live tile backgroung image. Not greater than 80KB as per MPNS specs. 
	tileData.SetBackBackgroundImage("http://cdn.shephertz.com/repository/files/760a0c04733f8d19bbec63cc5caebd585466ab0c2e63eed9219d601266a20eb9/398c5d95ac7585d27edb6ebe61d082128e52fd5f/PushTileWP.png");
	pushObj.SendPushTileMessageToUser(userId, tileData, callback);
 }

While using Tile Notifications we have to set “backBackgroundImage” and “backgroundImage”, either we can set a web Image URI or We can set the path of image existing at the device. Tile Notifications will appear on the main application title at HomeScreen of the device. When user clicks on that it opens the application.

From AppHQ Console

You can also send push messages to your registered app user directly from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Users
  • Select your App and user
  • Click on Manage from table and click on send push button as shown in the below image:

PushFromConsole

Type your message, once you are done click on send button as show below. This will trigger a push notification message to your app user

SendPushFromConsole

Once you are done click on send button. This will trigger a push notification message to your app user. You can also send tile notification from AppHQ by sending JSON formatted string e.g

{"tile":{"title":"Message","count":2,"backgroundImage":"<Your_remote_image_path>","backBackgroundImage":"Your_remote_image_path>","backTitle":"title","backContent":"content"}}
5. Sending Message to Channel

App42 Push notification also supports channel subscription model where user can subscribe on channel of his interest to receive the notification. You can send message to channel which will deliver message to all users who are subscribed to that channel. A channel can be created from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Channels
  • Select your App -> Click on Add channel button

AddChannelFromConsole

Once channel is created you can ask user for subscription on the channel. Below is the code snippet for the same

//
// First Subscribe User to Channel
//
String channelName = "cricket channel";  
String userName = "Nick";  
pushObj.SubscribeToChannel(channel,userName,this);
//
// Now send message on the Channel
//
String channelName = "cricket channel";  
String message = "Mumbai Indians won IPL 6";  
pushObj.SendPushMessageToChannel(channelName,message,this); 
6. Scheduling Message to User

You can also schedule message to your app user on specified time from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Users
  • Select your App -> select target user and click on manage
  • Click on Send button and type your message in pop up and pass your time to schedule the message.

SchedulePushMessageFromConsole

7. Scheduling message to Channel

Message scheduling on channel can be done in similar way. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Channels
  • Select your App -> select target channel and click on manage
  • Click on Send Push button and type your message in pop up and pass your time to schedule the message.

SchedulePushMessageFromConsole

8. Send Multilingual Push Notification

Once your device is registered for Push Notification you are ready to send multilingual(UTF-8) push message to your app user using following code snippet from any App42 SDK (Android/Java/WP etc)

String userName = "Nick";
String message = "Message which you have to send";
Dictionary<String, String> otherMetaHeaders = new Dictionary<String, String>();
otherMetaHeaders.Add("dataEncoding", "true");
pushNotificationService.SetOtherMetaHeaders(otherMetaHeaders);
pushNotificationService.SendPushMessageToUser(userName,message,new Callback());  
public class Callback : App42Callback  
{  
	 public void OnException(App42Exception exception)  
	  {  
		Console.WriteLine("Exception Message : " + exception);  
	  }  
	  public void OnSuccess(Object response)  
	  {  
		 PushNotification pushNotification = (PushNotification) response;     
		 String jsonResponse = pushNotification.ToString();  
	  }  
}  

For more details of Push Notification Documentation