Push Notification Windows Phone Example Code

Push Notification Windows Phone Example Code


using UnityEngine;
using System.Collections;
using PNC.WP.Core;

public class PushNotificationWPTest : MonoBehaviour
{
    private float yPosInterval = 40;
    private const string channelName = "MY_CHANNEL_NAME";
    public string response;

    private void OnEnable()
    {
        yPosInterval = Screen.height / 10;
        PushNotificationCrossWP.Init(channelName);
        PushNotificationCrossWP.Instance.IsDebug = true;    //Write All Logs in console !
        PushNotificationCrossWP.Instance.ChannelErrorEvent += OnChannelError;
        PushNotificationCrossWP.Instance.HttpNotificationReceivedEvent += OnHttpNotificationReceived;
        PushNotificationCrossWP.Instance.NotificationChannelUpdateEvent += OnNotificationChannelUpdate;
        PushNotificationCrossWP.Instance.ToasNotificationReceivedEvent += OnToasNotificationReceived;
    }

    private void OnDisable()
    {
        PushNotificationCrossWP.Instance.ChannelErrorEvent -= OnChannelError;
        PushNotificationCrossWP.Instance.HttpNotificationReceivedEvent -= OnHttpNotificationReceived;
        PushNotificationCrossWP.Instance.NotificationChannelUpdateEvent -= OnNotificationChannelUpdate;
        PushNotificationCrossWP.Instance.ToasNotificationReceivedEvent -= OnToasNotificationReceived;
    }

    private void OnApplicationQuit()
    {
        PushNotificationCrossWP.Instance.Dispose();
    }

    public void OnGUI()
    {
        Rect buttonRects = new Rect(10, 10, 200, yPosInterval);

        GUI.Label(new Rect(buttonRects.width + buttonRects.x + 10, 20, Screen.width - buttonRects.x - 10 - buttonRects.width, Screen.height - 20), response);

        if (GUI.Button(buttonRects, "BindToShellRaw"))
        {
            //Register for Raw Notification or start to listen channel
            PushNotificationCrossWP.Instance.BindToShellRaw();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "BindToShellTile"))
        {
            //Register for Tile Notification
            PushNotificationCrossWP.Instance.BindToShellTile();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "BindToShellToast"))
        {
            //Register for Toast Notification
            PushNotificationCrossWP.Instance.BindToShellToast();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "BindToShellAllNotifications"))
        {
            //Register Raw,Tile,Toast Notifications
            PushNotificationCrossWP.Instance.BindToShellAllNotifications();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "Properties"))
        {
            //Channel Properties
            response = string.Format("ChannelName : {0}\nChannelUri : {1}\nConnectionStatus: {2}\nIsShellTileBound :{3}\nIsShellToastBound: {4}\nLastToastMessageQuery:{5}",
                PushNotificationCrossWP.Instance.ChannelName, PushNotificationCrossWP.Instance.ChannelUri,
                PushNotificationCrossWP.Instance.ConnectionStatus, PushNotificationCrossWP.Instance.IsShellTileBound,
                PushNotificationCrossWP.Instance.IsShellToastBound,
                PushNotificationCrossWP.Instance.LastToastMessageQuery);
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "ClearTile"))
        {
            //Clear Tile Notification On Screen
            PushNotificationCrossWP.Instance.ClearTile();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "UnbindToShellTile"))
        {
            PushNotificationCrossWP.Instance.UnbindToShellTile();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "UnbindToShellToast"))
        {
            PushNotificationCrossWP.Instance.UnbindToShellToast();
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "Close"))
        {
            //Close Channel
            PushNotificationCrossWP.Instance.Close();
        }
    }

    #region Listeners

    private void OnToasNotificationReceived(PNC.WP.Utils.ToastNotificationReceivedEventArgs toastNotificationReceived)
    {
        response = "Toast Notification Received:\n";
        response += string.Format("Received Time :{0}\n ", toastNotificationReceived.NotificationReceiveTime);

        foreach (var item in toastNotificationReceived.Collection)
        {
            response += string.Format("{0} : {1}", item.Key, item.Value);
        }
    }

    private void OnNotificationChannelUpdate(PNC.WP.Utils.NotificationChannelUpdateEventArgs notificationChannelUpdate)
    {
        response = "OnNotificationChannelUpdate :\nChannelUri :" + notificationChannelUpdate.ChannelUri;
        Debug.Log(notificationChannelUpdate.ChannelUri);
    }

    private void OnHttpNotificationReceived(PNC.WP.Utils.HttpNotificationReceivedEventArgs httpNotificationReceivedEventArgs)
    {
        response = string.Format("HttpNotification Received:\nChannelUri : {0}\nMessage : {1}",
            httpNotificationReceivedEventArgs.ChannelUri, httpNotificationReceivedEventArgs.Message);
    }

    private void OnChannelError(PNC.WP.Utils.ChannelErrorEventArgs channelError)
    {
        response = string.Format("Channel Error Occur:\nErrorAdditionalData : {0}\nErrorCode : {1}\nErrorType: {2}\nMessage : {3}",
            channelError.ErrorAdditionalData, channelError.ErrorCode, channelError.ErrorType, channelError.Message);
    }

    #endregion
}


Leave a Reply

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