Add Push to your Unity Game

In case you missed it, we recently announced a new version of our Kii Unity SDK that supports push notifications when deploying to Android and iOS. This post focuses on how to set up your Unity game so Android players can receive push notifications. The steps for iOS are pretty similar. Big difference is while iOS uses Apple Push Notification Service (APNS), Android uses Google Cloud Messaging (GCM).

push-notification-3d

Set up the backend

Your first move is to tell Kii and Google you want to use push notifications in your game. It takes 5 detailed steps but they’re not hard.

  1. Create an application in your Kii developer console
  2. Install the Kii Unity libraries in your game
  3. Initialize the Kii backend by providing app id and key from the game
  4. Set up your app in Google for push
  5. Configure your game in the Kii developer console with Google’s GCM API key

Update your game

Our latest Unity SDK (v1.8.2+) has a new file called KiiPushPlugin.unitypackage that provides all the push functionality on the Unity side. To import it into your Unity game, double click on it while Unity is open. New files are added to the Plugins directory of your project. (You can uncheck the iOS specific files for this demo if you want to save space.)

Along with low-level push files, you’ll find a Plugins/KiiPushPlugin.cs file that provides the interface for push at the Unity level. Open Plugins/Android/AndroidManifest.xml and replace any instance of YOUR_APPLICATION_PACKAGE_NAME with the package name you specified in the Unity Build Settings for Android.

Important: when editing the manifest don’t touch the line <action android:name="PACKAGE_NAME.MESSAGE"/>

Now initialize KiiPushPlugin.cs by attaching it to a game object (e.g. attach it to the same object you used to initialize Kii via KiiInitializeBehavior). Then fill in the Sender ID public variable (only for Android) with the app id assigned to you by Google (Google apis console -> Dashboard -> Project ID).

Next you need some code on the Unity side to handle the push notifications. You could use something like this:

Code:

//Set up push listeners
KiiPushPlugin kiiPushPlugin = GameObject.Find ("KiiPushPlugin").GetComponent<KiiPushPlugin> ();
Debug.Log ("Found KiiPushPlugin object in game objects");
kiiPushPlugin.OnPushMessageReceived += (ReceivedMessage message) => {
	// This event handler is called when received the push message.
	switch (message.PushMessageType)
	{
	case ReceivedMessage.MessageType.PUSH_TO_APP:
		Debug.Log ("#####PUSH_TO_APP Message");
		// do something to notify your app of the incomig message
		break;
	case ReceivedMessage.MessageType.PUSH_TO_USER:
		Debug.Log ("#####PUSH_TO_USER Message");
		// your user received a message, do something
		break;
	case ReceivedMessage.MessageType.DIRECT_PUSH:
		Debug.Log ("#####DIRECT_PUSH Message");
		// A direct push message was sent from developer.kii.com
		// Let's grab the url value of the message and open that page
		string url = message.GetString("url");
		Debug.Log ("Url in message is: " + url);
		Application.OpenURL("http://" + url);
		break;
	}
	Debug.Log("Type=" + message.PushMessageType);
	Debug.Log("Sender=" + message.Sender);
	Debug.Log("Scope=" + message.ObjectScope);
	// You can get the value of custom field using GetXXXX method.
	Debug.Log("Payload=" + message.GetString("payload"));
};

#if UNITY_IPHONE
KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
#elif UNITY_ANDROID
KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#else
KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#endif

kiiPushPlugin.RegisterPush((string pushToken, Exception e0)=> {
	if (e0 != null) {
		Debug.Log("#####failed to RegisterPush");
		return;
	}
	Debug.Log ("#####RegistrationId=" + pushToken);
	Debug.Log ("#####Install");
	KiiUser.PushInstallation (true).Install (pushToken, deviceType, (Exception e3)=>{
		if (e3 != null)
		{
			Debug.Log ("#####failed to Install");
			return;
		}
	});
});

Test it out

Let’s try to send a direct push message from developer.kii.com to a logged in game player. In your console go to the Push section of you project and click the “New Message” button. Create a message like this:

directpush

While a player is in the game and you’re signed in, click “Send…” The URL will open. Success!

Wrap up

Summing it all up, here are the steps to adding push notifications to your Android game with the Kii Unity SDK:

  1. Import contents of KiiPushPlugin.unitypackage into your Unity game
  2. Replace package name in Plugins/Android/AndroidManifest.xml
  3. Create an empty GameObject and attach the KiiPushPlugin.cs and fill in the Sender ID
  4. Add something like the code above to handle push notifications (RegisterPush() and Install() are required)

To help you get started even faster, we’ve put together this source code of a simple game already set up to support push on Android.

Happy coding!