public void Setup()
        {
            // Listen to notification activation
            ToastNotificationManagerCompat.OnActivated += toastArgs =>
            {
                // Obtain the arguments from the notification
                ToastArguments args = ToastArguments.Parse(toastArgs.Argument);

                // Obtain any user input (text boxes, menu selections) from the notification
                ValueSet userInput = toastArgs.UserInput;

                // Need to dispatch to UI thread if performing UI operations
                Application.Current.Dispatcher.Invoke(delegate
                {
                    if (args.TryGetValue(_ActionTypeCode, out string value))
                    {
                        switch (value)
                        {
                        case _ActionTypeStartTask:
                            userInput.TryGetValue(_SelectionTaskKey, out object key);
                            StartSelectedWorkTask(key?.ToString());
                            break;

                        case _ActionTypeStartLunch:
                            StartBreak();
                            break;
                        }
                    }
                    else
                    {
                        ((MainWindow)Application.Current.MainWindow).ShowWindow();
                    }
                });
            };
        public void TestStronglyTyped()
        {
            ToastArguments args = new ToastArguments()
                                  .Add("isAdult", true)
                                  .Add("isPremium", false)
                                  .Add("age", 22)
                                  .Add("level", 0)
                                  .Add("gpa", 3.97)
                                  .Add("percent", 97.3f);

#if !WINRT
            args.Add("activationKind", ToastActivationType.Background);
#endif

            AssertEqual(
                new ToastArguments()
            {
                { "isAdult", "1" },
                { "isPremium", "0" },
                { "age", "22" },
                { "level", "0" },
                { "gpa", "3.97" },
                { "percent", "97.3" },
#if !WINRT
                { "activationKind", "1" }
#endif
            }, args);

            Assert.AreEqual(true, args.GetBool("isAdult"));
            Assert.AreEqual("1", args.Get("isAdult"));

            Assert.AreEqual(false, args.GetBool("isPremium"));
            Assert.AreEqual("0", args.Get("isPremium"));

            Assert.AreEqual(22, args.GetInt("age"));
            Assert.AreEqual(22d, args.GetDouble("age"));
            Assert.AreEqual(22f, args.GetFloat("age"));
            Assert.AreEqual("22", args.Get("age"));

            Assert.AreEqual(0, args.GetInt("level"));

            Assert.AreEqual(3.97d, args.GetDouble("gpa"));

            Assert.AreEqual(97.3f, args.GetFloat("percent"));

#if !WINRT
            Assert.AreEqual(ToastActivationType.Background, args.GetEnum <ToastActivationType>("activationKind"));

            if (args.TryGetValue("activationKind", out ToastActivationType activationType))
            {
                Assert.AreEqual(ToastActivationType.Background, activationType);
            }
            else
            {
                Assert.Fail("TryGetValue as enum failed");
            }

            // Trying to get enum that isn't an enum should return false
            Assert.IsFalse(args.TryGetValue("percent", out activationType));
#endif

            // After serializing and deserializing, the same should work
            args = ToastArguments.Parse(args.ToString());

            Assert.AreEqual(true, args.GetBool("isAdult"));
            Assert.AreEqual("1", args.Get("isAdult"));

            Assert.AreEqual(false, args.GetBool("isPremium"));
            Assert.AreEqual("0", args.Get("isPremium"));

            Assert.AreEqual(22, args.GetInt("age"));
            Assert.AreEqual(22d, args.GetDouble("age"));
            Assert.AreEqual(22f, args.GetFloat("age"));
            Assert.AreEqual("22", args.Get("age"));

            Assert.AreEqual(0, args.GetInt("level"));

            Assert.AreEqual(3.97d, args.GetDouble("gpa"));

            Assert.AreEqual(97.3f, args.GetFloat("percent"));

#if !WINRT
            Assert.AreEqual(ToastActivationType.Background, args.GetEnum <ToastActivationType>("activationKind"));

            if (args.TryGetValue("activationKind", out activationType))
            {
                Assert.AreEqual(ToastActivationType.Background, activationType);
            }
            else
            {
                Assert.Fail("TryGetValue as enum failed");
            }

            // Trying to get enum that isn't an enum should return false
            Assert.IsFalse(args.TryGetValue("percent", out activationType));
#endif
        }
Exemplo n.º 3
0
        private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
        {
            Dispatcher.Invoke(() =>
            {
                // If arguments are empty, that means the app title within Action Center was clicked.
                if (e.Argument.Length == 0)
                {
                    OpenWindowIfNeeded();
                    return;
                }

                // Parse the toast arguments
                ToastArguments args = ToastArguments.Parse(e.Argument);

                int conversationId = args.GetInt("conversationId");

                // If no specific action, view the conversation
                if (args.TryGetValue("action", out MyToastActions action))
                {
                    switch (action)
                    {
                    // View conversation
                    case MyToastActions.ViewConversation:

                        // Make sure we have a window open and in foreground
                        OpenWindowIfNeeded();

                        // And then show the conversation
                        (Current.Windows[0] as MainWindow).ShowConversation(conversationId);

                        break;

                    // Open the image
                    case MyToastActions.ViewImage:

                        // The URL retrieved from the toast args
                        string imageUrl = args["imageUrl"];

                        // Make sure we have a window open and in foreground
                        OpenWindowIfNeeded();

                        // And then show the image
                        (Current.Windows[0] as MainWindow).ShowImage(imageUrl);

                        break;

                    // Background: Quick reply to the conversation
                    case MyToastActions.Reply:

                        // Get the response the user typed
                        string msg = e.UserInput["tbReply"] as string;

                        // And send this message
                        ShowToast("Message sent: " + msg + "\nconversationId: " + conversationId);

                        // If there's no windows open, exit the app
                        if (Current.Windows.Count == 0)
                        {
                            Current.Shutdown();
                        }

                        break;

                    // Background: Send a like
                    case MyToastActions.Like:

                        ShowToast($"Like sent to conversation {conversationId}!");

                        // If there's no windows open, exit the app
                        if (Current.Windows.Count == 0)
                        {
                            Current.Shutdown();
                        }

                        break;

                    default:

                        OpenWindowIfNeeded();

                        break;
                    }
                }
            });
        }