Azure通知中心UWP,UWP Toast通知无法启动应用

时间:2017-01-18 09:33:07

标签: azure push-notification uwp

按照本教程,我正在使用UWP应用程序的通知中心:Getting started with Notification Hubs for Windows Universal Platform Apps

如果我使用Windows 8通知测试发送如下:

<?xml version="1.0" encoding="utf-8"?>
<toast>
<visual><binding template="ToastText01">
<text id="1">Test message</text>
</binding>
</visual>
</toast>

它有效,如果我点击通知,它会通过OnLaunched()方法打开应用程序。但是,如果我发送UWP特定通知,如:

<toast launch="app-defined-string">
<visual>
<binding template="ToastGeneric">
  <text>Microsoft Company Store</text>
  <text>New Halo game is back in stock!</text>
</binding>
</visual>
<actions>
<action activationType="foreground" content="See more details" arguments="details"/>
<action activationType="background" content="Remind me later" arguments="later"/>
</actions>
</toast>

吐司有效,但是如果我点击它,它会打开应用程序并且OnLaunched()从未调用过,因此应用程序会停留在spashscreen上。

先谢谢你的帮助,

此致

2 个答案:

答案 0 :(得分:2)

您需要在app.xaml.cs中实现OnActivated

appWidgetManager.partiallyUpdateAppWidget(i, remoteViews)

请参阅https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10/

答案 1 :(得分:0)

对于那些有同样问题的人,请使用Dave Smits回答:juste在App.xaml.cs文件中添加OnZctivated方法并将相同内容放在OnLaunched方法中:

protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        await OnLaunchedOrActivated(e);
    }

protected override async void OnActivated(IActivatedEventArgs e)
    {
        await OnLaunchedOrActivated(e);
    }

private async Task OnLaunchedOrActivated(IActivatedEventArgs e)
    {
        // Initialize things like registering background task before the app is loaded

        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        // Handle toast activation
        if (e is ToastNotificationActivatedEventArgs)
        {
            var toastActivationArgs = e as ToastNotificationActivatedEventArgs;

            // If empty args, no specific action (just launch the app)
            if (toastActivationArgs.Argument.Length == 0)
            {
                if (rootFrame.Content == null)
                    rootFrame.Navigate(typeof(MainPage));
            }
            // Otherwise an action is provided
            else
            {
                // Parse the query string


                // See what action is being requested

                // If we're loading the app for the first time, place the main page on the back stack
                // so that user can go back after they've been navigated to the specific page
                if (rootFrame.BackStack.Count == 0)
                    rootFrame.BackStack.Add(new PageStackEntry(typeof(MainPage), null, null));
            }
        }

        // Handle launch activation
        else if (e is LaunchActivatedEventArgs)
        {
            var launchActivationArgs = e as LaunchActivatedEventArgs;

            // If launched with arguments (not a normal primary tile/applist launch)
            if (launchActivationArgs.Arguments.Length > 0)
            {
                // TODO: Handle arguments for cases like launching from secondary Tile, so we navigate to the correct page
                throw new NotImplementedException();
            }

            // Otherwise if launched normally
            else
            {
                // If we're currently not on a page, navigate to the main page
                if (rootFrame.Content == null)
                    rootFrame.Navigate(typeof(MainPage));
            }
        }

        else
        {
            // TODO: Handle other types of activation
            throw new NotImplementedException();
        }


        // Ensure the current window is active
        Window.Current.Activate();
    }