【问题标题】:Azure notification hub UWP, UWP toast notifications don't launch appAzure 通知中心 UWP、UWP toast 通知不启动应用程序
【发布时间】:2017-06-02 14:44:32
【问题描述】:

我正在按照本教程为 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>

toast 可以工作,但如果我点击它,它会打开应用程序并且 OnLaunched() 从未调用过,因此应用程序卡在了闪屏上。

提前感谢您的帮助,

问候

【问题讨论】:

    标签: azure push-notification uwp


    【解决方案1】:

    你需要在你的 app.xaml.cs 中实现 OnActivated

     protected override void OnActivated(IActivatedEventArgs args)
            {
                base.OnActivated(args);
            }
    

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

    【讨论】:

      【解决方案2】:

      对于那些有同样问题的人,用 Dave Smits 回答:只需在 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();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-14
        • 2016-08-14
        • 1970-01-01
        • 2022-11-25
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 2016-09-30
        相关资源
        最近更新 更多