Permalink
Browse files

Merge pull request #1238 from mvegaca/Issue36-doc

Doc: Notifications in Windows Template Studio
  • Loading branch information...
ralarcon committed Oct 13, 2017
2 parents 13f5b94 + 6bda978 commit dae7d9dc8bc8c2f4d529680c238350dbe879efdf
Showing with 1,366 additions and 1 deletion.
  1. +1 −1 docs/getting-started-endusers.md
  2. +89 −0 docs/notifications.md
  3. +40 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample.sln
  4. +11 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/.editorconfig
  5. +34 −0 ...les/notifications/ToastNotificationSample/ToastNotificationSample/Activation/ActivationHandler.cs
  6. +38 −0 ...ions/ToastNotificationSample/ToastNotificationSample/Activation/DefaultLaunchActivationHandler.cs
  7. +18 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/App.xaml
  8. +45 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/App.xaml.cs
  9. BIN ...notifications/ToastNotificationSample/ToastNotificationSample/Assets/LockScreenLogo.scale-200.png
  10. BIN ...s/notifications/ToastNotificationSample/ToastNotificationSample/Assets/SplashScreen.scale-200.png
  11. BIN ...ifications/ToastNotificationSample/ToastNotificationSample/Assets/Square150x150Logo.scale-200.png
  12. BIN ...otifications/ToastNotificationSample/ToastNotificationSample/Assets/Square44x44Logo.scale-200.png
  13. BIN ...ificationSample/ToastNotificationSample/Assets/Square44x44Logo.targetsize-24_altform-unplated.png
  14. BIN samples/notifications/ToastNotificationSample/ToastNotificationSample/Assets/StoreLogo.png
  15. BIN ...otifications/ToastNotificationSample/ToastNotificationSample/Assets/Wide310x150Logo.scale-200.png
  16. +24 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Helpers/Observable.cs
  17. +57 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Helpers/RelayCommand.cs
  18. +17 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Helpers/ResourceExtensions.cs
  19. +19 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Helpers/Singleton.cs
  20. +58 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Package.appxmanifest
  21. +30 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Properties/AssemblyInfo.cs
  22. +31 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Properties/Default.rd.xml
  23. +116 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Services/ActivationService.cs
  24. +86 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Services/NavigationService.cs
  25. +65 −0 ...ons/ToastNotificationSample/ToastNotificationSample/Services/ToastNotificationsService.Samples.cs
  26. +24 −0 ...tifications/ToastNotificationSample/ToastNotificationSample/Services/ToastNotificationsService.cs
  27. +134 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Strings/en-us/Resources.resw
  28. +22 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Styles/TextBlock.xaml
  29. +5 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Styles/_Colors.xaml
  30. +8 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Styles/_FontSizes.xaml
  31. +16 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Styles/_Thickness.xaml
  32. +230 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/ToastNotificationSample.csproj
  33. BIN ...ications/ToastNotificationSample/ToastNotificationSample/ToastNotificationSample_TemporaryKey.pfx
  34. +37 −0 ...cations/ToastNotificationSample/ToastNotificationSample/ViewModels/ActivatedFromToastViewModel.cs
  35. +13 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/ViewModels/MainViewModel.cs
  36. +26 −0 ...s/notifications/ToastNotificationSample/ToastNotificationSample/Views/ActivatedFromToastPage.xaml
  37. +25 −0 ...otifications/ToastNotificationSample/ToastNotificationSample/Views/ActivatedFromToastPage.xaml.cs
  38. +29 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Views/MainPage.xaml
  39. +18 −0 samples/notifications/ToastNotificationSample/ToastNotificationSample/Views/MainPage.xaml.cs
@@ -7,6 +7,7 @@ The final generated code is the result of a project configuration (project type
- [Application activation](activation.md)
- [Navigation between pages](navigation.md)
- [Notifications in Windows Template Studio](notifications.md)
## Understanding concepts for Windows Template Studio
@@ -69,7 +70,6 @@ Windows Template Studio approaches UWP app creation using the following four att
| Uri Scheme | Add the ability to launch and deep link into the app with a custom URI scheme.|
## Table of Contents
* [Installing / Using the extension](getting-started-extension.md)
* [**Using and extending your file->new**](getting-started-endusers.md)
* [Concepts of Windows Template Studio](readme.md)
View
@@ -0,0 +1,89 @@
# Notifications on Windows Template Studio
Windows Template Studio supports three different type of notifications for UWP:
- Toast notifications
- Hub notifications
- Store notifications
## Toast notifications
ToastNotificationService is in change of sending notifications directly from code in the application. The service contains a method `ShowToastNotification` that sends a notification to the Windows action center. The feature code also includes a `ShowToastNotificationSample` class that shows how to create and send a notification from code.
ToastNotificationsService extends `ActivationHandler` to handle application activation from a toast notification. This code is not implemented on the template because the logic is application dependent. The following [ToastNotificationSample](/samples/notifications/ToastNotificationSample) contains an example of one way to handle application activation from a toast notification.
Check out the [activation documentation](activation.md) to learn more about handling app activation.
The relevant parts of the sample app that handle activation are shown below.
```csharp
// ToastNotificationService.cs
protected override async Task HandleInternalAsync(ToastNotificationActivatedEventArgs args)
{
// Handle the app activation from a ToastNotification
NavigationService.Navigate<Views.ActivatedFromToastPage>(args);
await Task.CompletedTask;
}
// ActivatedFromToastPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ViewModel.Initialize(e.Parameter as ToastNotificationActivatedEventArgs);
}
// ActivatedFromToastViewModel.cs
public void Initialize(ToastNotificationActivatedEventArgs args)
{
// Check args looking for information about the toast notification
if (args.Argument == "ToastButtonActivationArguments")
{
// ToastNotification was clicked on OK Button
ActivationSource = "ActivationSourceButtonOk".GetLocalized();
}
else if(args.Argument == "ToastContentActivationParams")
{
// ToastNotification was clicked on main content
ActivationSource = "ActivationSourceContent".GetLocalized();
}
}
```
Full toast notification documentation for UWP [here](https://developer.microsoft.com/en-us/windows/uwp-community-toolkit/api/microsoft_toolkit_uwp_notifications_toastcontent).
## Hub notifications
HubNotificationsService is in change of configuring the application with the Azure notifications service to allow the application to receive push notifications from a remote service in Azure. The service contains the `InitializeAsync` method that sets up the Hub Notifications. You must specify the hub name and the access signature before start working with Hub Notifications. There is more documentation about how to create and connect an Azure notifications service [here](https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-windows-store-dotnet-get-started-push).
Toast Notifications sent from Azure notification service shoudl be handled in the same way as locally generated ones. See the above referenced [ToastNotificationSample](/samples/notifications/ToastNotificationSample) for more.
## Store notifications
StoreNotificationsService is in change of configuring the application with the Windows Dev Center notifications service to allow the application to receive push notifications from Windows Dev Center remote service. The service contains the `InitializeAsync` method that sets up the Store Notifications. This feature use the Store API to configure the notifications.
See the official documentation on how to [configure your app for targeted push notifications](https://docs.microsoft.com/windows/uwp/monetize/configure-your-app-to-receive-dev-center-notifications) and how to [send notifications to your app's customers](https://docs.microsoft.com/windows/uwp/publish/send-push-notifications-to-your-apps-customers).
To handle app activation from a Toast Notification that is sent from Windows Dev Center service will need you to add a similar implementation to that detailed above. THe key difference is to call `ParseArgumentsAndTrackAppLaunch` to notify the Windows Dev Center that the app was launched in response to a targeted push notification and to get the original arguments. An example of this is shown below.
```csharp
protected override async Task HandleInternalAsync(ToastNotificationActivatedEventArgs args)
{
var toastActivationArgs = args as ToastNotificationActivatedEventArgs;
StoreServicesEngagementManager engagementManager = StoreServicesEngagementManager.GetDefault();
string originalArgs = engagementManager.ParseArgumentsAndTrackAppLaunch(toastActivationArgs.Argument);
//// Use the originalArgs variable to access the original arguments passed to the app.
NavigationService.Navigate<Views.ActivatedFromStorePage>(originalArgs);
await Task.CompletedTask;
}
```
Other interesting links about notifications
- [Buttons in Toast Notifications](https://developer.microsoft.com/en-us/windows/uwp-community-toolkit/api/microsoft_toolkit_uwp_notifications_toastbutton)
- [Toast Notification class](https://docs.microsoft.com/uwp/api/windows.ui.notifications.toastnotification)
- [UWP Toast notification when application is in foreground](https://social.msdn.microsoft.com/Forums/en-US/ff8acad4-f0c2-4a36-ac90-84780276fd09/uwptoast-notification-when-application-is-in-foreground)
- [Suppress Toast Notification when app is in the foreground](https://social.msdn.microsoft.com/Forums/en-US/21a374dc-6510-48ea-b058-a9d4424cda4b/uwpc-suppress-toast-notification-when-app-is-in-the-foreground)
- [Windows traffic app sample](https://github.com/microsoft/windows-appsample-trafficapp/)
- [Windows notifications samples](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Notifications)
- [Windows toast notifications sample](https://github.com/WindowsNotifications/quickstart-sending-local-toast-win10)
- [Send push notifications from Windows Developer Center](https://docs.microsoft.com/windows/uwp/publish/send-push-notifications-to-your-apps-customers)
- [Handle toast notification activation](https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10/)
- [Adding push notifications](https://docs.microsoft.com/azure/app-service-mobile/app-service-mobile-windows-store-dotnet-get-started-push)
- [Configure your app for targeted push notifications](https://docs.microsoft.com/windows/uwp/monetize/configure-your-app-to-receive-dev-center-notifications)
- [Send notifications to your app's customers](https://docs.microsoft.com/windows/uwp/publish/send-push-notifications-to-your-apps-customers)
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26206.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToastNotificationSample", "ToastNotificationSample\ToastNotificationSample.csproj", "{A9AFABA1-53BC-4E38-8B10-536A04A98166}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM = Release|ARM
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution {A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|ARM.ActiveCfg = Debug|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|ARM.Build.0 = Debug|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|ARM.Deploy.0 = Debug|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x64.ActiveCfg = Debug|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x64.Build.0 = Debug|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x64.Deploy.0 = Debug|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x86.ActiveCfg = Debug|x86
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x86.Build.0 = Debug|x86
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Debug|x86.Deploy.0 = Debug|x86
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|ARM.ActiveCfg = Release|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|ARM.Build.0 = Release|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|ARM.Deploy.0 = Release|ARM
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x64.ActiveCfg = Release|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x64.Build.0 = Release|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x64.Deploy.0 = Release|x64
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x86.ActiveCfg = Release|x86
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x86.Build.0 = Release|x86
{A9AFABA1-53BC-4E38-8B10-536A04A98166}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,11 @@
# top-most EditorConfig file
root = true
[*]
end_of_line = crlf
[*.{cs,xaml}]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;
namespace ToastNotificationSample.Activation
{
// For more information on application activation see https://github.com/Microsoft/WindowsTemplateStudio/blob/master/docs/activation.md
internal abstract class ActivationHandler
{
public abstract bool CanHandle(object args);
public abstract Task HandleAsync(object args);
}
internal abstract class ActivationHandler<T> : ActivationHandler
where T : class
{
protected abstract Task HandleInternalAsync(T args);
public override async Task HandleAsync(object args)
{
await HandleInternalAsync(args as T);
}
public override bool CanHandle(object args)
{
return args is T && CanHandleInternal(args as T);
}
protected virtual bool CanHandleInternal(T args)
{
return true;
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using ToastNotificationSample.Helpers;
using ToastNotificationSample.Services;
using Windows.ApplicationModel.Activation;
namespace ToastNotificationSample.Activation
{
internal class DefaultLaunchActivationHandler : ActivationHandler<LaunchActivatedEventArgs>
{
private readonly Type _navElement;
public DefaultLaunchActivationHandler(Type navElement)
{
_navElement = navElement;
}
protected override async Task HandleInternalAsync(LaunchActivatedEventArgs args)
{
// When the navigation stack isn't restored, navigate to the first page and configure
// the new page by passing required information in the navigation parameter
NavigationService.Navigate(_navElement, args.Arguments);
// TODO WTS: This is a sample on how to show a toast notification.
// You can use this sample to create toast notifications where needed in your app.
Singleton<ToastNotificationsService>.Instance.ShowToastNotificationSample();
await Task.CompletedTask;
}
protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
{
// None of the ActivationHandlers has handled the app activation
return NavigationService.Frame.Content == null;
}
}
}
@@ -0,0 +1,18 @@
<Application
x:Class="ToastNotificationSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/_Colors.xaml"/>
<ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
<ResourceDictionary Source="/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="/Styles/TextBlock.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
@@ -0,0 +1,45 @@
using System;
using ToastNotificationSample.Services;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
namespace ToastNotificationSample
{
public sealed partial class App : Application
{
private Lazy<ActivationService> _activationService;
private ActivationService ActivationService
{
get { return _activationService.Value; }
}
public App()
{
InitializeComponent();
// Deferred execution until used. Check https://msdn.microsoft.com/library/dd642331(v=vs.110).aspx for further info on Lazy<T> class.
_activationService = new Lazy<ActivationService>(CreateActivationService);
}
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
if (!args.PrelaunchActivated)
{
await ActivationService.ActivateAsync(args);
}
}
protected override async void OnActivated(IActivatedEventArgs args)
{
await ActivationService.ActivateAsync(args);
}
private ActivationService CreateActivationService()
{
return new ActivationService(this, typeof(Views.MainPage));
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace ToastNotificationSample.Helpers
{
public class Observable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
OnPropertyChanged(propertyName);
}
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
@@ -0,0 +1,57 @@
using System;
using System.Windows.Input;
namespace ToastNotificationSample.Helpers
{
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action execute)
: this(execute, null)
{
}
public RelayCommand(Action execute, Func<bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute == null || _canExecute();
public void Execute(object parameter) => _execute();
public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute == null || _canExecute((T)parameter);
public void Execute(object parameter) => _execute((T)parameter);
public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
@@ -0,0 +1,17 @@
using System;
using System.Runtime.InteropServices;
using Windows.ApplicationModel.Resources;
namespace ToastNotificationSample.Helpers
{
internal static class ResourceExtensions
{
private static ResourceLoader _resLoader = new ResourceLoader();
public static string GetLocalized(this string resourceKey)
{
return _resLoader.GetString(resourceKey);
}
}
}
Oops, something went wrong.

0 comments on commit dae7d9d

Please sign in to comment.