Windows Phone Push notification

Push Notification helps you in sending event or alert to your app users even when they are not logged into 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 notification 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 stated with App42 Push Notification using sample project in few minutes.

1. Start building

bullet Download sample project from our GitHub Repo

bullet Unzip the downloaded file and open it in Microsoft Visual Studio Express for Windows Phone.

bullet Edit MainPage.xaml.cs file and put your API KEY and SECRET KEY as shown below and also modify userId variable with your App user name

ServiceAPI sp = new ServiceAPI("<YOUR API KEY>","<YOUR SECRET KEY>");
String userId = "<Your App User>";

If you want to go in 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 notification. 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.

3. Sending Tost 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);
        }

3. 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 device. Tile Notifications will appear on main application tile at HomeScreen of 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, go to AppHQ console and click on Push Notification -> Click on User Link -> Select your App -> select your user from table and click on send message button. This will open a pop up to type your message, 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"}}

4. 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 create from AppHQ console. Go to Push Notification -> Channel -> Select your App -> Click on Create Channel button

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); 

5. Scheduling Message to User

You can also schedule message to your app user on specified time from AppHQ console. To do this, got to Push Notification -> Click on User link -> Select your app -> select target users/device and click on schedule message button.Now type your message in pop up and pass your time to schedule the message.

6. Scheduling message to Channel

Message scheduling on channel can be done in similar way. Go to Push Notification -> Click on Channel link -> Select your app -> select target channel and click on schedule message button.Now type your message in pop up and pass your time to schedule the message.