Unity3d: Android and iOS Push Notifications

11,518

You are right, someone has done that indeed :) I'm one of developers of the new Unity asset UTNotifications. It does all you need and even more. Please also note that there is no single way to implement the Push Notifications to work with any Android device - there is Google Cloud Messaging (GCM) which works only with Google Play based devices and Amazon Device Messaging (ADM) for the Amazon devices. Fortunately, the UTNotifications supports both the services and also the Apple Push Notification Service (APNS) for iOS. Its complete source code is provided so you can adjust anything you like. It also includes the demo server source code so you'll be able to use your own server for sending push notifications with no need to use any 3rd party service.

More info: http://forum.unity3d.com/threads/released-utnotifications-professional-cross-platform-push-notifications-and-more.333045/

Here are some code samples.
For example, here is how you can initialize the system and send the push notifications registration id to the server:

public void Start()
{
    UTNotifications.Manager notificationsManager = UTNotifications.Manager.Instance;
    notificationsManager.OnSendRegistrationId += SendRegistrationId;

    bool result = notificationsManager.Initialize(false);
    Debug.Log("UTNotifications Initialize: " + result);
}

private void SendRegistrationId(string providerName, string registrationId)
{
    StartCoroutine(_SendRegistrationId(providerName, registrationId));
}

private IEnumerator _SendRegistrationId(string providerName, string registrationId)
{
    WWWForm wwwForm = new WWWForm();

    wwwForm.AddField("provider", providerName);
    wwwForm.AddField("id", registrationId);

    WWW www = new WWW(m_webServerAddress + "/register", wwwForm);
    yield return www;

    if (www.error != null)
    {
        Debug.LogError(www.error);
    }
}

And this is how you request the demo server to send a push notification to every registered device from Unity:

public IEnumerator NotifyAll(string title, string text)
{
    title = WWW.EscapeURL(title);
    text = WWW.EscapeURL(text);

    WWW www = new WWW(m_webServerAddress + "/notify?title=" + title + "&text=" + text);
    yield return www;
}

You can also process incoming notifications:

UTNotifications.Manager.Instance.OnNotificationsReceived += (receivedNotifications) =>
{
    Debug.Log(receivedNotifications[0].userData["CustomMessage"]);
};

You can do lots of other things with a similar syntax. The asset includes the SampleUI class. It will help you a lot. There are also the API Reference (http://universal-tools.github.io/UTNotifications/html/annotated.html) and the detailed manual.

Share:
11,518
dubadub
Author by

dubadub

make code not war

Updated on June 04, 2022

Comments

  • dubadub
    dubadub almost 2 years

    I'm working on a mobile game with Unity and going to use push notifications. I found NotificationServices class that for iOS only. But I didn't find any server-side code examples for that class.

    I'm asking for any nice examples or solutions providing server and client-side code (Android client & backend for both the Android & iOS).

    I know services such as pushwoosh. They didn't work for me since my company has its own game server and would like to send notifications from it.

    I guess it's not something unique and someone should already do that.

    Thank you.