Developers: Integrating Push Notification in Unity on Windows Phone

Reading time icon 4 min. read


Readers help support MSpoweruser. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help MSPoweruser sustain the editorial team Read more

shep

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:

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

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

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.
push  notification unity 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.

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.

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 [email protected].

More about the topics: unity push in windows phone, unity push notification, unity push notification windows, unity3d push, windows phone push notification, wp7 push notification, wp8 push notification