Skip to content

Commit 4552caa

Browse files
authored
Copy notifications code to sample (#147)
* Copy notifications code to sample * Button is not needed
1 parent 6741b31 commit 4552caa

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

samples/RNWinRTTestApp/App.tsx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
import React from "react";
@@ -10,18 +10,21 @@ import {
1010
View,
1111
Text,
1212
StatusBar,
13+
Pressable,
1314
} from "react-native";
1415

1516
import {
1617
Colors,
1718
Header,
1819
} from "react-native/Libraries/NewAppScreen";
1920

21+
import {showNotification} from './Notifications'
22+
2023
async function updateJumpListAsync(): Promise<void> {
2124
try {
2225
const StartScreenApi = Windows.UI.StartScreen;
2326
const jumplist = await StartScreenApi.JumpList.loadCurrentAsync();
24-
27+
2528
jumplist.systemGroupKind = StartScreenApi.JumpListSystemGroupKind.recent;
2629

2730
const items = jumplist.items;
@@ -97,7 +100,7 @@ async function getPictureThumbnailAsync(): Promise<string> {
97100

98101
const library = StorageApi.KnownFolders.picturesLibrary;
99102
let files = await library.getFilesAsync();
100-
103+
101104
let file: StorageApi.StorageFile | null = null;
102105
if (files.size > 0) {
103106
file = files.getAt(0);
@@ -199,6 +202,27 @@ const App = () => {
199202
</Text>
200203
</View>
201204
</View>
205+
<View style={styles.body}>
206+
<View style={styles.sectionContainer}>
207+
<Text style={styles.sectionTitle}>Windows.UI.Notifications Example</Text>
208+
<View style={{ flexDirection: 'row', flexWrap: 'wrap', alignItems: 'center' }}>
209+
<Text style={[{ paddingRight: 10 }, styles.sectionDescription]}>Click the button to show a notification: </Text>
210+
<Pressable style={styles.sectionDescriptionButton} onPress={() => {
211+
showNotification({
212+
template: Windows.UI.Notifications.ToastTemplateType.toastImageAndText01,
213+
// The template schema can be found at https://docs.microsoft.com/previous-versions/windows/apps/hh761494(v=win.10)
214+
text: "hello world",
215+
image: {
216+
src: "https://microsoft.github.io/react-native-windows/img/header_logo.svg",
217+
alt: "React logo",
218+
}
219+
});
220+
}}>
221+
<Text style={styles.sectionDescriptionButtonText}>Press</Text>
222+
</Pressable>
223+
</View>
224+
</View>
225+
</View>
202226
<View style={styles.body}>
203227
<View style={styles.sectionContainer}>
204228
<Text style={styles.sectionTitle}>Windows.Storage API + picturesLibrary capability Example</Text>
@@ -241,6 +265,19 @@ const styles = StyleSheet.create({
241265
fontWeight: "400",
242266
color: Colors.dark,
243267
},
268+
sectionDescriptionButton: {
269+
alignItems: 'center',
270+
justifyContent: 'center',
271+
paddingVertical: 4,
272+
paddingHorizontal: 24,
273+
borderRadius: 1,
274+
backgroundColor: Colors.light,
275+
},
276+
sectionDescriptionButtonText: {
277+
fontSize: 18,
278+
fontWeight: "400",
279+
color: Colors.dark,
280+
},
244281
highlight: {
245282
fontWeight: "700",
246283
},
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
const Notifications = Windows.UI.Notifications;
3+
const ToastTemplateType = Notifications.ToastTemplateType;
4+
const ToastNotificationManager = Notifications.ToastNotificationManager;
5+
const ToastNotification = Notifications.ToastNotification;
6+
7+
export function showNotification(notification) {
8+
var type = ToastTemplateType.toastText01;
9+
10+
var obj = {};
11+
if (typeof(notification) == 'string') {
12+
obj['text'] = notification;
13+
} else {
14+
obj = notification;
15+
}
16+
17+
if (obj.template != undefined) {
18+
type = obj.template;
19+
}
20+
21+
var xml = ToastNotificationManager.getTemplateContent(type);
22+
for (var tagName in obj) {
23+
var xmlElements = xml.getElementsByTagName(tagName);
24+
var value = obj[tagName];
25+
if (typeof(value) == 'string') {
26+
fillXmlElements(xml, xmlElements, [value]);
27+
} else if (Array.isArray(value)) {
28+
fillXmlElements(xml, xmlElements, value);
29+
} else if (typeof(value) == 'object') {
30+
fillXmlElements(xml, xmlElements, [value]);
31+
}
32+
}
33+
34+
var toast = new ToastNotification(xml);
35+
ToastNotificationManager.createToastNotifier().show(toast);
36+
}
37+
38+
function fillXmlElements(xml, xmlElements, arr) {
39+
var i = 0;
40+
for (var arrValue of arr) {
41+
var node = xmlElements[i++];
42+
if (typeof(arrValue) == 'string') {
43+
node.appendChild(xml.createTextNode(arrValue));
44+
} else if (typeof(arrValue) == 'object') {
45+
for (var attrName in arrValue) {
46+
var attr = node.attributes.getNamedItem(attrName);
47+
if (!attr) {
48+
attr = xml.createAttribute(attrName);
49+
node.attributes.setNamedItem(attr);
50+
}
51+
52+
attr.nodeValue = arrValue[attrName];
53+
}
54+
}
55+
}
56+
}

samples/RNWinRTTestApp/windows/WinRTTurboModule/WinRTTurboModule.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
2222
<CppWinRTGenerateWindowsMetadata>true</CppWinRTGenerateWindowsMetadata>
2323
<CppWinRTParameters>-base</CppWinRTParameters>
24-
<RnWinRTParameters>-include Windows.Foundation -include Windows.UI.StartScreen -include Windows.UI.ViewManagement -include Windows.Storage -include Windows.Security.Cryptography.CryptographicBuffer -include Windows.Security.Cryptography.ICryptographicBufferStatics -include Windows.Security.Cryptography.BinaryStringEncoding</RnWinRTParameters>
24+
<RnWinRTParameters>-include Windows.Foundation -include Windows.UI.StartScreen -include Windows.UI.ViewManagement -include Windows.Storage -include Windows.Security.Cryptography.CryptographicBuffer -include Windows.Security.Cryptography.ICryptographicBufferStatics -include Windows.Security.Cryptography.BinaryStringEncoding -include Windows.UI.Notifications -include Windows.Data.Xml.Dom</RnWinRTParameters>
2525
</PropertyGroup>
2626
<PropertyGroup Label="ReactNativeWindowsProps">
2727
<ReactNativeWindowsDir Condition="'$(ReactNativeWindowsDir)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(SolutionDir), 'node_modules\react-native-windows\package.json'))\node_modules\react-native-windows\</ReactNativeWindowsDir>

0 commit comments

Comments
 (0)