Push notifications added to Kii Unity SDK

PushYou pushed for it and now it’s here! Support for Push Notifications in our Unity SDK v1.7. Stay even closer to your players with personalized messages and notify your game of meaningful events, including changes on the backend.

 3 ways to use push with Kii Unity SDK

  1. Direct Push notifications Send a push notification to a particular player or group of players. For example, to notify them about a limited-time discount on a virtual item or game asset.
  2. Push to User notifications Enable push notifications between your players. For example, set up a chat or messaging system.
  3. Push to App notifications Tell your game an event has occurred. For example, that a new level is available for download.

Set up push notifications for Unity

To get started, set up GCM (Android) or APNS (iOS), depending on your build platform, and then install the Kii Push Plugin Unity package. Our easy-to-follow documentation for adding push notifications to your game and managing push notifications walk you through everything in detail.

Sample code

Being able to notify your game of changes with Push to App is one of the most useful push notification capabilities. Say you want to add a new level. Once push notifications are set up, subscribe the current player to changes in the “levels” bucket where you store your game level metadata. When you make changes to the bucket or object, you can use push to notify your game and have it connect to a server to download the new level.

Let’s take a look at how to let the game receive a notification when a new level is available. The snippet below does the following:

  1. Search the Game Object, with the KiiPushPlugin plugin bound, using the GameObject.Find method.
  2. Get the instance of the bound KiiPushPlugin with the GameObject.GetComponent method.
  3. Add the event handler for receiving the push notifications on the OnPushMessageReceived event.
  4. Determine the push message type with the PushMessageType property.
    (If the message is “Push to App”, the type will be “PUSH_TO_APP”).
  5. Get the predefined and application specific field values from the ReceivedMessage.
KiiUser user = KiiUser.CurrentUser;
KiiBucket bucket = Kii.Bucket("levels");
KiiPushSubscription ps = user.PushSubscription;
ps.Subscribe(bucket, (KiiSubscribable target, Exception e) => {
  if (e != null)
  {
    // check fail cause
  }
});

Then, in order to enable the reception of those notifications, we use something like this (either in a new or an existing class):

public class Main : MonoBehaviour
{
  private KiiPushPlugin kiiPushPlugin = null;

  void Awake()
  {
    Kii.Initialize("___APP_ID___", "___APP_KEY___", Kii.Site.US);
  }

  void Start()
  {
    this.kiiPushPlugin = GameObject.Find("KiiPushPlugin").GetComponent<KiiPushPlugin>();
    this.kiiPushPlugin.OnPushMessageReceived += (ReceivedMessage message) => {
      // This event handler is called when received the push message.
      switch (message.PushMessageType)
      {
        case ReceivedMessage.MessageType.PUSH_TO_APP:
          // Get the "push_to_app" specific fields.
          PushToAppMessage appMsg = (PushToAppMessage)message;
          Debug.Log("Bucket=" + appMsg.KiiBucket.Uri.ToString());
          Debug.Log("Object=" + appMsg.KiiObject.Uri.ToString());
          break;
        case ReceivedMessage.MessageType.PUSH_TO_USER:
          // "Push to User" message handling...
          break;
        case ReceivedMessage.MessageType.DIRECT_PUSH:
          // "Direct push" message handling...
          break;
      }
      // Get the value of common fields.
      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"));
    };
  }
}

The last step above allows us to see the bucket and object that have changed. If there’s a new level, we can go ahead and have the game connect to a server to download it.

Questions? Comments? We’re talking about using push notifications with Unity over on the Kii Community boards. Let us know how you added push support to your Unity game and if you need any help troubleshooting, let us know.

- Share -

German Viscuso

More posts from

  • Kii IoT platform enables customers to create smart, connected solutions and services
  • Long track record of supporting customers like Docomo, Toshiba, Kyocera and many others
  • Strong ecosystem partners across the globe, consisting of carriers, device/sensor manufacturers, system integrators, network/infrastructure providers, chip vendors and solution developers
  • HQ in Tokyo, US HQ in Silicon Valley & offices in China, Taiwan, Hong Kong and Europe (UK, Spain, Germany)
  • www.kii.com