Integrating Push Notification in Unity on Windows Phone

If you are developing unity application for windows phone and willing to integrate push notification service you can do it easily by using App42 API. We have develop a sample game on Unity3D to explain the integration steps of push notification service using App42 API. Here are the steps to get started with this sample.

Running Sample:
1. Register with App42 platform.
2. Create an app once you are on Quick start page after registration.
3. If you are already a registered user then login to AppHQ console and create an app from App Manager Tab.
4. Download the project from here
5. Open Constants.cs file in assets folder of sample app and make the following changes:

1
2
A. Replace api-Key and secret-Key that you have received from AppHq.
B. Replace your user-id by which you want to register your application for PushNotification

6. Build and Run the application.
7. On first Run of the application it gets registered with MPNS as well as App42 Push Notification Service.

Test and verify PushNotification from AppHQ console

1
2
3
A. After registering for PushNotification go to AppHQ console and click on PushNotification and select application after selecting User tab.
B. Select your user from registered User-list and click on Send Message Button.
C. Send appropriate message to the user by clicking Send Button.

Send push to user using API App42 provides APIs to send the push notification to any user by his userid or send the push notifications to all users as shown below. We get the response in App42Calback

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void sendPushToUser(string userName, string msg) {
        App42API.BuildPushNotificationService().SendPushMessageToUser(userName, msg, new Callback());
 }
 
public void sendPushToAll(string msg) {
        App42API.BuildPushNotificationService().SendPushMessageToAll(msg, new Callback());
 }
 
public class Callback : App42CallBack{
	public static string response = "";
	public void OnSuccess(object obj){
		response = obj.ToString();
	}
	public void OnException(Exception ex){
		Debug.Log("EDITEOR---"+ ex.ToString());
		response=ex.ToString();
	}
}

Design Details

Initialization and Registration To Implement push notification service on windows phone we have to get Push Notification URI from MPNS server.
Slide1 Integrating Push Notification in Unity on Windows Phone
Our plugin has the CreatePushChannel API to complete above explained flow.In this API we give userid,PushChannelRegistrationCallback and PushChannelMessageCallback. We get the response of channel URI in PushChannelRegistrationCallback. Once we get push notification URI we register it with App42 Push Notification Service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Start()
{
     m_ScreenRectangle = new Rect(0, 0, Screen.width, 100);
     m_GUIStyle = new GUIStyle {fontSize = 16, alignment = TextAnchor.MiddleCenter,wordWrap=true};
     #if UNITY_WP8
     App42API.Initialize(Constants.ApiKey,Constants.SecretKey);
     App42API.SetLoggedInUser(Constants.UserId);
     App42PushService pushService=new App42PushService();
     pushService.CreatePushChannel(Constants.UserId,PushChannelRegistrationCallback,PushChannelMessageCallback);
     message="Push Channel Start";
      #endif
}
void PushChannelRegistrationCallback(object msg, bool IsError) {
        message = (string) msg;
        if (!IsError) {
            StoreDeviceId((string) msg);
        }
        Debug.Log(message);
}
public void StoreDeviceId(string pushUri) {
        App42API.BuildPushNotificationService().StoreDeviceToken(App42API.GetLoggedInUser(), pushUri,
                Constants.deviceType, new Callback());
}

PushChannelMessageCallback is used to receive Push notifications.Since we do receive push notification in dictionary format we have to decode information by keys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void PushChannelMessageCallback(object sender, Dictionary<string, string> e) {
        // Parse out the information that was part of the message.
        StringBuilder msg = new StringBuilder();
        foreach(string key in e.Keys)
		{
	   msg.AppendFormat("{0}: {1}\n", key, e[key]);
            //if (string.Compare(
            //   key,
            //  "wp:Param",
            //  System.Globalization.CultureInfo.InvariantCulture,
            //  System.Globalization.CompareOptions.IgnoreCase) == 0)
            //{
            //    relativeUri = e.Collection[key];
            //}
        }
        message = msg.ToString();
        Debug.Log(message);
}

Integrating Push in your Existing Unity Project

Download the App42 Unity API ,take the App42_Unity3D_SDK_WP8.dll and  put  it in asset folder.
Download the UnityPushNotificationPlugin Fake Dll and put it in Asset Folder.
Download the UnityPushNotificationPlugin Real Dll and put it in Asset/Pulgin/WP8 folder. You have to call CreatePushChannel method as explained in above on start up of the app.

Note:Fake dll and Real dll concept is as per unity project design for including windows phone dlls.Read more here.

Complete source code of the game can be downloaded or viewed from our Git repo. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.

twitter Integrating Push Notification in Unity on Windows Phonefacebook Integrating Push Notification in Unity on Windows Phonegoogle Integrating Push Notification in Unity on Windows Phonelinkedin Integrating Push Notification in Unity on Windows Phonepinterest Integrating Push Notification in Unity on Windows Phonereddit Integrating Push Notification in Unity on Windows Phonestumbleupon Integrating Push Notification in Unity on Windows Phonetumblr Integrating Push Notification in Unity on Windows Phone

Leave a Reply

Your email address will not be published. Required fields are marked *


nine − 7 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>