Original: Quick build Windows 8 style app 34-Build Toast Notifications
Introduction
Developers who have developed windowsphone applications or have used WindowsPhone phones know that we will receive some application prompts that appear at the top of the phone and will disappear after a few seconds. Of course, if we click on the popup information will automatically run the corresponding application, this message we call notification.
In fact, the Windows 8 Store app also draws on the information on the Windows Phone, and our developers can make their app pop-up at the right time, so these Windows 8 store app prompts, we're called toast notifications.
and the toast notification style can be customized. For example:
I. Overview of TOAST Notifications
A TOAST notification is a notification that appears in the upper-right corner of the screen (in the upper-left corner of the right-to-left (right-to-left) direction).
- Toast notifications can provide temporary messages outside the app context
- Use toast notifications to instantly draw users ' attention
- Users can permanently turn off the app's toast notifications
- Ability to quickly navigate to context-sensitive locations in your app
- Easily invoke toast notifications from the app on-premises or in the cloud
Toast notifications use a template architecture that is similar to live tiles, with a rich representation to choose from.
More about Toast Notifications Overview can be consulted: Toast Notification Overview (Windows Store app) (Windows)
Second, toast notification build
1, declaring the Toast notification feature
Open the Package.appxmanifest app manifest file, and find the support toast notification setting to Yes on the Application UI tab.
2, declaring Toast notification background color and text color
We can set the background color and light or dark text for Toast notifications. Note: This setting applies both toast and tile notification backgrounds as well as text color.
3, specify small logo image
The app's small logo image appears in the lower-right corner of each Toast notification, allowing the user to identify the app that raised the notification.
4, adding namespace declarations
1: using Windows.UI.Notifications;
2: using Windows.Data.Xml.Dom;
5, pick a template for Toast and retrieve its XML content
We select a suitable template from the system-provided template directory (a detailed list of templates can refer to the Toasttemplatetype enumeration).
Note: Each individual notification that we send can use a different template.
1:
2: XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplate);
Here I am using the ToastImageAndText01 template, which is styled as follows:
6. Provide text content for notifications
We can first retrieve all the elements of the template named "Text".
The ToastImageAndText01 template contains only one text string that the code assigns. The string can contain up to three lines of wrapped string, so we should set the length of the string accordingly to avoid truncation.
1: xmlnodelist toasttextelements = toastxml.getelementsbytagname ("text");
2: toasttextelements[0]. AppendChild (Toastxml.createtextnode ("Hello Windows 8!"));
7. Provide images for notifications
We can first retrieve all the elements of the template named "Image".
Unlike tiles, Toast templates (such as ToastImageAndText01) contain up to one image.
Note: Not all Toast templates contain images, and some tile templates are text-only.
1: xmlnodelist toastimageattributes = toastxml.getelementsbytagname ("image");
Then we can use the image from the app's package, local storage, or from the Web. Note: The image size is less than 1024 KB, which is less than a few pixels.
Here I take the image of the application package as an example:
1: ((XmlElement) toastimageattributes[0]). SetAttribute ("src""ms-appx:///assets/smalllogo.png");
2: ((XmlElement) toastimageattributes[0]). SetAttribute ("alt""Red graphic");
8, specify toast duration or toast audio (not necessary)
We can set the duration for the toast (only two values: short and long), and we usually use a "long" value when our notification belongs to an appointment or a meeting reminder.
Note: Toast duration defaults to "short".
1: ixmlnode toastnode = Toastxml.selectsinglenode ("/toast");
2: ((XmlElement) toastnode). SetAttribute ("duration""Long");
We can also set toast Audio, which, by default, plays a short sound when the notification is played. At the same time we can choose to use the system-provided sound, or not to use any sound.
Because the audio element is not included in the template, we must define the element and use the "ms-winsoundevent:" prefix to specify the sound file.
1: ixmlnode toastnode = Toastxml.selectsinglenode ("/toast");
2: XmlElement audio = toastxml.createelement ("audio");
Specifies a non-default sound.
1: audio. SetAttribute ("src""ms-winsoundevent:notification.im");
After you define the audio element, you need to attach it to the XML payload of the toast as a child of the toast element.
1: toastnode.appendchild (audio);
9, specifying the application's startup parameters
Typically when a user taps a Toast notification, the app should start and display a view related to the content of the notification.
We can use the launch property of the Toast element to achieve this effect.
This property provides a string that is passed from the toast to the app when the app is launched through a toast. This string does not have any specific form, it is used by us to define.
Our app must check this string as a parameter each time it is activated and adjust its view or operation accordingly. For example:
1: ((XmlElement) toastnode). SetAttribute ("launch""{\" type\ ": \" toast\ ", \" param1\ ": \" 12345\ ", \" param2\ ": \" 67890\ "}");
10, create a TOAST notification and send
1: new toastnotification (toastxml);
2: toastnotificationmanager.createtoastnotifier (). Show (toast);
The final run effect can be as follows:
A toast notification pops up when the button is clicked:
For more information on toast notifications, refer to:
1, Send Toast notifications (Windows store apps that use c#/vb/c++ and XAML) (Windows)
2,toast Notifications Sample
Quickly build Windows 8 style apps 34-Build toast Notifications