Browse Source

Initial basic support for action buttons

Sebastián Katzer 8 years ago
parent
commit
15bb160fac

+ 17 - 1
src/windows/LocalNotificationProxy.js

@@ -60,7 +60,7 @@ exports.request = function (success, error) {
  * @return [ Void ]
  */
 exports.schedule = function (success, error, args) {
-    var options = [];
+    var options = [], buttons = [];
 
     for (var i = 0, props, opts; i < args.length; i++) {
         props = args[i];
@@ -72,6 +72,22 @@ exports.schedule = function (success, error, args) {
             }
         }
 
+        for (var j = 0, action; j < props.actions.length; j++) {
+            action = props.actions[j];
+
+            if (!action.type || action.type == 'button') {
+                var button = new LocalNotification.Button();
+
+                button.id     = action.id;
+                button.title  = action.title;
+                button.launch = action.launch;
+
+                buttons.push(button);
+            }
+        }
+
+        opts.buttons = buttons;
+
         options.push(opts);
     }
 

+ 10 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs

@@ -75,6 +75,11 @@ namespace LocalNotificationProxy.LocalNotification
 
                         AppLogoOverride = this.Options.ToastLogo
                     }
+                },
+
+                Actions = new ToastActionsCustom()
+                {
+                    Buttons = { }
                 }
             };
 
@@ -83,6 +88,11 @@ namespace LocalNotificationProxy.LocalNotification
                 toast.Visual.BindingGeneric.Children.Add(image);
             }
 
+            foreach (var btn in this.Options.ToastButtons)
+            {
+                (toast.Actions as ToastActionsCustom).Buttons.Add(btn);
+            }
+
             var xml = toast.GetXml();
             var at = this.Options.TriggerDate;
             ScheduledToastNotification notification;

+ 57 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Button.cs

@@ -0,0 +1,57 @@
+/*
+ * Apache 2.0 License
+ *
+ * Copyright (c) Sebastian Katzer 2017
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apache License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ */
+
+namespace LocalNotificationProxy.LocalNotification
+{
+    using Microsoft.Toolkit.Uwp.Notifications;
+
+    public sealed class Button
+    {
+        /// <summary>
+        /// Gets or sets button ID.
+        /// </summary>
+        public string ID { get; set; }
+
+        /// <summary>
+        /// Gets or sets button title.
+        /// </summary>
+        public string Title { get; set; }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether to launch the app.
+        /// </summary>
+        public bool Launch { get; set; }
+
+        /// <summary>
+        /// Gets the parsed toast button.
+        /// </summary>
+        internal ToastButton ToastButton
+        {
+            get
+            {
+                return new ToastButton(this.Title, this.ID)
+                {
+                    ActivationType = this.Launch ? ToastActivationType.Foreground : ToastActivationType.Background
+                };
+            }
+        }
+    }
+}

+ 24 - 3
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs

@@ -22,14 +22,12 @@
 namespace LocalNotificationProxy.LocalNotification
 {
     using System;
+    using System.Collections.Generic;
     using Microsoft.Toolkit.Uwp.Notifications;
     using Windows.Data.Xml.Dom;
-    using System.Collections;
-    using System.Collections.Generic;
 
     public sealed class Options
     {
-
         /// <summary>
         /// Gets or sets notification ID.
         /// </summary>
@@ -80,6 +78,11 @@ namespace LocalNotificationProxy.LocalNotification
         /// </summary>
         public string[] Attachments { get; set; }
 
+        /// <summary>
+        /// Gets or sets the notification buttons.
+        /// </summary>
+        public Button[] Buttons { get; set; }
+
         /// <summary>
         /// Gets the date when to trigger the notification.
         /// </summary>
@@ -303,6 +306,24 @@ namespace LocalNotificationProxy.LocalNotification
             }
         }
 
+        /// <summary>
+        /// Gets all toast buttons.
+        /// </summary>
+        internal List<ToastButton> ToastButtons
+        {
+            get
+            {
+                var buttons = new List<ToastButton>();
+
+                foreach (var action in this.Buttons)
+                {
+                    buttons.Add(action.ToastButton);
+                }
+
+                return buttons;
+            }
+        }
+
         /// <summary>
         /// Deserializes the XML string into an instance of Options.
         /// </summary>

+ 1 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.csproj

@@ -112,6 +112,7 @@
   <ItemGroup>
     <Compile Include="LocalNotificationProxy.cs" />
     <Compile Include="LocalNotification\Builder.cs" />
+    <Compile Include="LocalNotification\Button.cs" />
     <Compile Include="LocalNotification\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 1 - 1
www/local-notification-util.js

@@ -33,7 +33,7 @@ exports._defaults = {
     data:    undefined,
     every:   undefined,
     at:      undefined,
-    actions: undefined,
+    actions: [],
     actionGroupId: undefined,
     attachments: []
 };