Commit aef10cac authored by Nadya Atanasova's avatar Nadya Atanasova Committed by EddyVerbruggen
Browse files

Add unified handling of raw and toast notifications

Add API for showing toast notifications, using the system behaviour.
Expose interface for users to handle raw and toast notifications by js callbacks.
This way they can handle notifications differently than the system
parent 14e811d6
Loading
Loading
Loading
Loading
+1 −0
Changes for plugin.xml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -139,6 +139,7 @@
    </config-file>

    <source-file src="src/wp8/PushPlugin.cs" />
    <framework src="src/wp8/Newtonsoft.Json.dll" custom="true" />

  </platform>

+366 KiB

File added.

No diff preview for this file type.

+63 −37
Changes for src/wp8/PushPlugin.cs: 63 added lines, 37 removed lines.
Original line number Diff line number Diff line
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Windows;
using WPCordovaClassLib.Cordova.JSON;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace WPCordovaClassLib.Cordova.Commands
{
@@ -27,7 +29,7 @@ namespace WPCordovaClassLib.Cordova.Commands
            if (pushChannel == null)
            {
                pushChannel = new HttpNotificationChannel(this.pushOptions.ChannelName);
                SubscribePushChannelEvents(pushChannel);

                try
                {
                    pushChannel.Open();
@@ -42,6 +44,7 @@ namespace WPCordovaClassLib.Cordova.Commands
                pushChannel.BindToShellTile();
            }

            SubscribePushChannelEvents(pushChannel);
            var result = new RegisterResult
            {
                ChannelName = this.pushOptions.ChannelName,
@@ -74,6 +77,18 @@ namespace WPCordovaClassLib.Cordova.Commands
            }
        }

        public void showToastNotification(string options)
        {
            ShellToast toast;
            if (!TryDeserializeOptions(options, out toast))
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
                return;
            }

            Deployment.Current.Dispatcher.BeginInvoke(toast.Show);
        }

        void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
        {
            // return uri to js
@@ -82,7 +97,7 @@ namespace WPCordovaClassLib.Cordova.Commands
                ChannelName = this.pushOptions.ChannelName,
                Uri = e.ChannelUri.ToString()
            };
            this.ExecuteCallback(this.pushOptions.UriChangedCallback, JsonHelper.Serialize(result));
            this.ExecuteCallback(this.pushOptions.UriChangedCallback, JsonConvert.SerializeObject(result));
        }

        void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
@@ -93,32 +108,40 @@ namespace WPCordovaClassLib.Cordova.Commands
                Code = e.ErrorCode.ToString(),
                Message = e.Message
            };
            this.ExecuteCallback(this.pushOptions.ErrorCallback, JsonHelper.Serialize(err));
			this.ExecuteCallback(this.pushOptions.ErrorCallback, JsonConvert.SerializeObject(err));
        }

        void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
        {
            StringBuilder message = new StringBuilder();
            string relativeUri = string.Empty;
            var toast = new PushNotification
            {
                Type = "toast"
            };

            Toast toast = new Toast();
            if (e.Collection.ContainsKey("wp:Text1"))
            foreach (var item in e.Collection)
            {
                toast.Title = e.Collection["wp:Text1"];
                toast.JsonContent.Add(item.Key, item.Value);
            }
            if (e.Collection.ContainsKey("wp:Text2"))
            {
                toast.Subtitle = e.Collection["wp:Text2"];

            this.ExecuteCallback(this.pushOptions.NotificationCallback, JsonConvert.SerializeObject(toast));
        }
            if (e.Collection.ContainsKey("wp:Param"))

        void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
        {
            var raw = new PushNotification
            {
                Type = "raw"
            };

            using (var reader = new StreamReader(e.Notification.Body))
            {
                toast.Param = e.Collection["wp:Param"];
                raw.JsonContent.Add("Body", reader.ReadToEnd());
            }

            this.ExecuteCallback(this.pushOptions.NotificationCallback, JsonHelper.Serialize(toast));
			this.ExecuteCallback(this.pushOptions.NotificationCallback, JsonConvert.SerializeObject(raw));
        }

        private void ExecuteCallback(string callback, string callbackResult)
        void ExecuteCallback(string callback, string callbackResult)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
@@ -145,13 +168,13 @@ namespace WPCordovaClassLib.Cordova.Commands
            });
        }

        private static bool TryDeserializeOptions<T>(string options, out T result) where T : class
        static bool TryDeserializeOptions<T>(string options, out T result) where T : class
        {
            result = null;
            try
            {
                var args = JsonHelper.Deserialize<string[]>(options);
                result = JsonHelper.Deserialize<T>(args[0]);
                var args = JsonConvert.DeserializeObject<string[]>(options);
                result = JsonConvert.DeserializeObject<T>(args[0]);
                return true;
            }
            catch
@@ -160,30 +183,18 @@ namespace WPCordovaClassLib.Cordova.Commands
            }
        }

        private static bool TryCast<T>(object obj, out T result) where T : class
        static bool TryCast<T>(object obj, out T result) where T : class
        {
            result = obj as T;
            return result != null;
        }

        private void SubscribePushChannelEvents(HttpNotificationChannel channel)
        void SubscribePushChannelEvents(HttpNotificationChannel channel)
        {
            channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
            channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
            channel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
        }

        [DataContract]
        public class Toast
        {
            [DataMember(Name = "text1", IsRequired = false)]
            public string Title { get; set; }

            [DataMember(Name = "text2", IsRequired = false)]
            public string Subtitle { get; set; }

            [DataMember(Name = "param", IsRequired = false)]
            public string Param { get; set; }
            channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
        }

        [DataContract]
@@ -212,6 +223,21 @@ namespace WPCordovaClassLib.Cordova.Commands
            public string ChannelName { get; set; }
        }

        [DataContract]
        public class PushNotification
        {
            public PushNotification()
            {
                this.JsonContent = new Dictionary<string, object>();
            }

            [DataMember(Name = "jsonContent", IsRequired = true)]
            public IDictionary<string, object> JsonContent { get; set; }

            [DataMember(Name = "type", IsRequired = true)]
            public string Type { get; set; }
        }

        [DataContract]
        public class RegisterError
        {
+12 −0
Changes for www/PushNotification.js: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,18 @@ PushNotification.prototype.unregister = function(successCallback, errorCallback,

     cordova.exec(successCallback, errorCallback, "PushPlugin", "unregister", [options]);
};

    // Call this if you want to show toast notification on WP8
    PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
        if (errorCallback == null) { errorCallback = function () { } }

        if (typeof errorCallback != "function") {
            console.log("PushNotification.register failure: failure parameter not a function");
            return
        }

        cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
    }
// Call this to set the application icon badge
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
    if (errorCallback == null) { errorCallback = function() {}}