expo-widgets 58.0.15 → 58.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/android/build.gradle +7 -2
- package/android/proguard-rules.pro +7 -0
- package/android/src/main/AndroidManifest.xml +6 -1
- package/android/src/main/java/expo/modules/widgets/ExpoWidgetEmittableTree.kt +7 -7
- package/android/src/main/java/expo/modules/widgets/ExpoWidgetsPeekWidget.kt +1 -1
- package/android/src/main/java/expo/modules/widgets/WidgetInteractionAction.kt +34 -4
- package/android/src/main/java/expo/modules/widgets/WidgetsEvents.kt +8 -4
- package/android/src/main/java/expo/modules/widgets/WidgetsModule.kt +45 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotification.kt +28 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationBuilder.kt +116 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationExceptions.kt +50 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationFactory.kt +36 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationOptions.kt +86 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationRenderer.kt +44 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationState.kt +65 -0
- package/android/src/main/java/expo/modules/widgets/notifications/CustomNotifications.kt +107 -0
- package/android/src/main/java/expo/modules/widgets/notifications/ExpoWidgetsNotificationReceiver.kt +67 -0
- package/android/src/main/res/values/expo_widgets_notifications.xml +4 -0
- package/build/CustomNotifications.d.ts +128 -0
- package/build/CustomNotifications.js +162 -0
- package/build/CustomNotifications.js.map +1 -0
- package/build/CustomNotifications.types.d.ts +124 -0
- package/build/CustomNotifications.types.js +3 -0
- package/build/CustomNotifications.types.js.map +1 -0
- package/build/ExpoWidgets.d.ts +3 -0
- package/build/ExpoWidgets.js +26 -0
- package/build/ExpoWidgets.js.map +1 -1
- package/build/ExpoWidgets.native.d.ts +5 -0
- package/build/ExpoWidgets.native.js.map +1 -1
- package/build/Widgets.d.ts +14 -0
- package/build/Widgets.js +14 -0
- package/build/Widgets.js.map +1 -1
- package/build/Widgets.types.d.ts +24 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/bundle/decorator.ts +13 -0
- package/bundle/index.ts +19 -2
- package/bundle/layout-registry-stub.ts +19 -0
- package/ios/ExpoWidgets.podspec +5 -7
- package/ios/Widgets/EntryView.swift +17 -1
- package/ios/Widgets/RedBoxView.swift +16 -0
- package/layout-registry.metro.config.js +9 -2
- package/package.json +4 -4
- package/plugin/build/android/withAndroidNotificationPermission.d.ts +6 -0
- package/plugin/build/android/withAndroidNotificationPermission.js +31 -0
- package/plugin/build/android/withAndroidNotificationPermission.js.map +1 -0
- package/plugin/build/android/withAndroidWidgets.d.ts +4 -2
- package/plugin/build/android/withAndroidWidgets.js +6 -1
- package/plugin/build/android/withAndroidWidgets.js.map +1 -1
- package/plugin/build/ios/withWidgetSourceFiles.js +1 -1
- package/plugin/build/withWidgets.js +1 -0
- package/plugin/build/withWidgets.js.map +1 -1
- package/plugin/src/android/withAndroidNotificationPermission.ts +22 -0
- package/plugin/src/android/withAndroidWidgets.ts +11 -1
- package/plugin/src/ios/withWidgetSourceFiles.ts +1 -1
- package/plugin/src/withWidgets.ts +2 -2
- package/src/CustomNotifications.ts +213 -0
- package/src/CustomNotifications.types.ts +138 -0
- package/src/ExpoWidgets.native.ts +8 -0
- package/src/ExpoWidgets.ts +41 -0
- package/src/Widgets.ts +14 -0
- package/src/Widgets.types.ts +24 -0
- package/src/index.ts +8 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ConfigPlugin } from 'expo/config-plugins';
|
|
2
|
+
type NotificationPermissionProps = {
|
|
3
|
+
enablePushNotifications: boolean;
|
|
4
|
+
};
|
|
5
|
+
declare const withAndroidNotificationPermission: ConfigPlugin<NotificationPermissionProps>;
|
|
6
|
+
export default withAndroidNotificationPermission;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function _configplugins() {
|
|
12
|
+
const data = require("expo/config-plugins");
|
|
13
|
+
_configplugins = function() {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
const withAndroidNotificationPermission = (config, props)=>{
|
|
19
|
+
if (!props.enablePushNotifications) {
|
|
20
|
+
return config;
|
|
21
|
+
}
|
|
22
|
+
return (0, _configplugins().withAndroidManifest)(config, (config)=>{
|
|
23
|
+
_configplugins().AndroidConfig.Permissions.ensurePermissions(config.modResults, [
|
|
24
|
+
'android.permission.POST_NOTIFICATIONS'
|
|
25
|
+
]);
|
|
26
|
+
return config;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const _default = withAndroidNotificationPermission;
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=withAndroidNotificationPermission.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["android/withAndroidNotificationPermission.ts"],"sourcesContent":[null],"names":["withAndroidNotificationPermission","config","props","enablePushNotifications","withAndroidManifest","AndroidConfig","Permissions","ensurePermissions","modResults"],"mappings":";;;;+BAqBA;;;eAAA;;;;yBArBiE;;;;;;AAMjE,MAAMA,oCAA+E,CACnFC,QACAC;IAEA,IAAI,CAACA,MAAMC,uBAAuB,EAAE;QAClC,OAAOF;IACT;IACA,OAAOG,IAAAA,oCAAmB,EAACH,QAAQ,CAACA;QAClCI,8BAAa,CAACC,WAAW,CAACC,iBAAiB,CAACN,OAAOO,UAAU,EAAE;YAC7D;SACD;QACD,OAAOP;IACT;AACF;MAEA,WAAeD"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ConfigPlugin } from 'expo/config-plugins';
|
|
2
2
|
import { WidgetConfig } from '../types/WidgetConfig.type';
|
|
3
|
-
|
|
3
|
+
type AndroidWidgetsProps = {
|
|
4
|
+
enablePushNotifications?: boolean;
|
|
4
5
|
widgets: WidgetConfig[];
|
|
5
|
-
}
|
|
6
|
+
};
|
|
7
|
+
declare const withAndroidWidgets: ConfigPlugin<AndroidWidgetsProps>;
|
|
6
8
|
export default withAndroidWidgets;
|
|
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "default", {
|
|
|
8
8
|
return _default;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _withAndroidNotificationPermission = /*#__PURE__*/ _interop_require_default(require("./withAndroidNotificationPermission"));
|
|
11
12
|
const _withAndroidWidgetFiles = /*#__PURE__*/ _interop_require_default(require("./withAndroidWidgetFiles"));
|
|
12
13
|
const _withAndroidWidgetManifest = /*#__PURE__*/ _interop_require_default(require("./withAndroidWidgetManifest"));
|
|
13
14
|
function _interop_require_default(obj) {
|
|
@@ -15,7 +16,11 @@ function _interop_require_default(obj) {
|
|
|
15
16
|
default: obj
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
|
-
const withAndroidWidgets = (config,
|
|
19
|
+
const withAndroidWidgets = (config, props)=>{
|
|
20
|
+
const { widgets } = props;
|
|
21
|
+
config = (0, _withAndroidNotificationPermission.default)(config, {
|
|
22
|
+
enablePushNotifications: props.enablePushNotifications ?? false
|
|
23
|
+
});
|
|
19
24
|
if (widgets.length === 0) {
|
|
20
25
|
return config;
|
|
21
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["android/withAndroidWidgets.ts"],"sourcesContent":[null],"names":["withAndroidWidgets","config","widgets","length","androidWidgets","filter","widget","android","nextConfig","withAndroidWidgetFiles","withAndroidWidgetManifest"],"mappings":";;;;+
|
|
1
|
+
{"version":3,"sources":["android/withAndroidWidgets.ts"],"sourcesContent":[null],"names":["withAndroidWidgets","config","props","widgets","withAndroidNotificationPermission","enablePushNotifications","length","androidWidgets","filter","widget","android","nextConfig","withAndroidWidgetFiles","withAndroidWidgetManifest"],"mappings":";;;;+BA8BA;;;eAAA;;;0FA3B8C;+EACX;kFACG;;;;;;AAOtC,MAAMA,qBAAwD,CAACC,QAAQC;IACrE,MAAM,EAAEC,OAAO,EAAE,GAAGD;IACpBD,SAASG,IAAAA,0CAAiC,EAACH,QAAQ;QACjDI,yBAAyBH,MAAMG,uBAAuB,IAAI;IAC5D;IACA,IAAIF,QAAQG,MAAM,KAAK,GAAG;QACxB,OAAOL;IACT;IAEA,MAAMM,iBAAiBJ,QAAQK,MAAM,CAAC,CAACC,SAAWA,OAAOC,OAAO,KAAK;IACrE,IAAIC,aAAaV;IACjB,IAAIM,eAAeD,MAAM,GAAG,GAAG;QAC7BK,aAAaC,IAAAA,+BAAsB,EAACD,YAAYJ;IAClD;IAEA,OAAOM,IAAAA,kCAAyB,EAACF,YAAYJ;AAC/C;MAEA,WAAeP"}
|
|
@@ -437,7 +437,7 @@ struct ${widget.name}EntryView: View {
|
|
|
437
437
|
let node = evaluateLayout(layout: layout, props: entry.props, environment: widgetEnvironment)
|
|
438
438
|
WidgetsDynamicView(name: entry.name, kind: .widget, node: node, entryIndex: entry.entryIndex, environmentString: widgetEnvironmentString)
|
|
439
439
|
} else {
|
|
440
|
-
|
|
440
|
+
EmptyWidgetView()
|
|
441
441
|
}
|
|
442
442
|
}
|
|
443
443
|
}
|
|
@@ -27,6 +27,7 @@ const withWidgets = (config, props)=>{
|
|
|
27
27
|
const enableAndroid = props?.enableAndroid ?? false;
|
|
28
28
|
const widgets = props?.widgets ?? [];
|
|
29
29
|
const nextConfig = enableAndroid ? (0, _withAndroidWidgets.default)(config, {
|
|
30
|
+
...props,
|
|
30
31
|
widgets
|
|
31
32
|
}) : config;
|
|
32
33
|
return (0, _withIosWidgets.default)(nextConfig, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["withWidgets.ts"],"sourcesContent":[null],"names":["pkg","require","withWidgets","config","props","enableAndroid","widgets","nextConfig","withAndroidWidgets","withIosWidgets","createRunOncePlugin","name","version"],"mappings":";;;;+BAiCA;;;eAAA;;;;yBAjCkD;;;;;;2EAEnB;uEACJ;;;;;;AAG3B,MAAMA,MAAMC,QAAQ;AAmBpB,MAAMC,cAAsE,CAACC,QAAQC;IACnF,MAAMC,gBAAgBD,OAAOC,iBAAiB;IAC9C,MAAMC,UAAUF,OAAOE,WAAW,EAAE;IAEpC,MAAMC,aAAaF,gBAAgBG,IAAAA,2BAAkB,EAACL,QAAQ;
|
|
1
|
+
{"version":3,"sources":["withWidgets.ts"],"sourcesContent":[null],"names":["pkg","require","withWidgets","config","props","enableAndroid","widgets","nextConfig","withAndroidWidgets","withIosWidgets","createRunOncePlugin","name","version"],"mappings":";;;;+BAiCA;;;eAAA;;;;yBAjCkD;;;;;;2EAEnB;uEACJ;;;;;;AAG3B,MAAMA,MAAMC,QAAQ;AAmBpB,MAAMC,cAAsE,CAACC,QAAQC;IACnF,MAAMC,gBAAgBD,OAAOC,iBAAiB;IAC9C,MAAMC,UAAUF,OAAOE,WAAW,EAAE;IAEpC,MAAMC,aAAaF,gBAAgBG,IAAAA,2BAAkB,EAACL,QAAQ;QAAE,GAAGC,KAAK;QAAEE;IAAQ,KAAKH;IACvF,OAAOM,IAAAA,uBAAc,EAACF,YAAY;QAAE,GAAGH,KAAK;QAAEE;IAAQ;AACxD;MAEA,WAAeI,IAAAA,oCAAmB,EAACR,aAAaF,IAAIW,IAAI,EAAEX,IAAIY,OAAO"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AndroidConfig, ConfigPlugin, withAndroidManifest } from 'expo/config-plugins';
|
|
2
|
+
|
|
3
|
+
type NotificationPermissionProps = {
|
|
4
|
+
enablePushNotifications: boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const withAndroidNotificationPermission: ConfigPlugin<NotificationPermissionProps> = (
|
|
8
|
+
config,
|
|
9
|
+
props
|
|
10
|
+
) => {
|
|
11
|
+
if (!props.enablePushNotifications) {
|
|
12
|
+
return config;
|
|
13
|
+
}
|
|
14
|
+
return withAndroidManifest(config, (config) => {
|
|
15
|
+
AndroidConfig.Permissions.ensurePermissions(config.modResults, [
|
|
16
|
+
'android.permission.POST_NOTIFICATIONS',
|
|
17
|
+
]);
|
|
18
|
+
return config;
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default withAndroidNotificationPermission;
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { ConfigPlugin } from 'expo/config-plugins';
|
|
2
2
|
|
|
3
3
|
import { WidgetConfig } from '../types/WidgetConfig.type';
|
|
4
|
+
import withAndroidNotificationPermission from './withAndroidNotificationPermission';
|
|
4
5
|
import withAndroidWidgetFiles from './withAndroidWidgetFiles';
|
|
5
6
|
import withAndroidWidgetManifest from './withAndroidWidgetManifest';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
type AndroidWidgetsProps = {
|
|
9
|
+
enablePushNotifications?: boolean;
|
|
10
|
+
widgets: WidgetConfig[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const withAndroidWidgets: ConfigPlugin<AndroidWidgetsProps> = (config, props) => {
|
|
14
|
+
const { widgets } = props;
|
|
15
|
+
config = withAndroidNotificationPermission(config, {
|
|
16
|
+
enablePushNotifications: props.enablePushNotifications ?? false,
|
|
17
|
+
});
|
|
8
18
|
if (widgets.length === 0) {
|
|
9
19
|
return config;
|
|
10
20
|
}
|
|
@@ -473,7 +473,7 @@ struct ${widget.name}EntryView: View {
|
|
|
473
473
|
let node = evaluateLayout(layout: layout, props: entry.props, environment: widgetEnvironment)
|
|
474
474
|
WidgetsDynamicView(name: entry.name, kind: .widget, node: node, entryIndex: entry.entryIndex, environmentString: widgetEnvironmentString)
|
|
475
475
|
} else {
|
|
476
|
-
|
|
476
|
+
EmptyWidgetView()
|
|
477
477
|
}
|
|
478
478
|
}
|
|
479
479
|
}
|
|
@@ -11,7 +11,7 @@ export type ExpoWidgetsConfigPluginProps = {
|
|
|
11
11
|
bundleIdentifier?: string;
|
|
12
12
|
// App group identifier used for communication between the main app and widgets. Defaults to `group.<main app bundle identifier>`.
|
|
13
13
|
groupIdentifier?: string;
|
|
14
|
-
// Enable push notifications for
|
|
14
|
+
// Enable push notifications for Live Activities and custom notifications. Defaults to false.
|
|
15
15
|
enablePushNotifications?: boolean;
|
|
16
16
|
// Enable frequent updates for widgets. Defaults to false.
|
|
17
17
|
frequentUpdates?: boolean;
|
|
@@ -27,7 +27,7 @@ const withWidgets: ConfigPlugin<ExpoWidgetsConfigPluginProps | undefined> = (con
|
|
|
27
27
|
const enableAndroid = props?.enableAndroid ?? false;
|
|
28
28
|
const widgets = props?.widgets ?? [];
|
|
29
29
|
|
|
30
|
-
const nextConfig = enableAndroid ? withAndroidWidgets(config, { widgets }) : config;
|
|
30
|
+
const nextConfig = enableAndroid ? withAndroidWidgets(config, { ...props, widgets }) : config;
|
|
31
31
|
return withIosWidgets(nextConfig, { ...props, widgets });
|
|
32
32
|
};
|
|
33
33
|
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { UnavailabilityError, uuid } from 'expo';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
CustomNotificationComponent,
|
|
6
|
+
CustomNotificationOptions,
|
|
7
|
+
CustomNotificationUpdateOptions,
|
|
8
|
+
NativeCustomNotification,
|
|
9
|
+
NativeCustomNotificationFactory,
|
|
10
|
+
} from './CustomNotifications.types';
|
|
11
|
+
import ExpoWidgetsModule from './ExpoWidgets';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A posted custom notification. It wraps a native object that knows the notification key, so an
|
|
15
|
+
* instance recovered with `CustomNotificationFactory.get()` or `getActive()` after a process restart
|
|
16
|
+
* behaves like the one returned by `post()`.
|
|
17
|
+
* @platform android
|
|
18
|
+
*/
|
|
19
|
+
export class CustomNotification<T extends object = object> {
|
|
20
|
+
/** @hidden */
|
|
21
|
+
private readonly nativeNotification: NativeCustomNotification;
|
|
22
|
+
|
|
23
|
+
/** @hidden */
|
|
24
|
+
constructor(nativeNotification: NativeCustomNotification) {
|
|
25
|
+
this.nativeNotification = nativeNotification;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The key of the notification, as passed to or generated by `post()`.
|
|
30
|
+
* @platform android
|
|
31
|
+
*/
|
|
32
|
+
get id(): string {
|
|
33
|
+
return this.nativeNotification.getId();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Reads the props currently shown by the notification from the active notification, so they
|
|
38
|
+
* include changes made by button presses inside the notification.
|
|
39
|
+
*
|
|
40
|
+
* Rejects when the notification is no longer active, for example after dismissal or cancellation.
|
|
41
|
+
* @returns A promise that resolves to the current props.
|
|
42
|
+
* @platform android
|
|
43
|
+
*/
|
|
44
|
+
async getProps(): Promise<T> {
|
|
45
|
+
return (await this.nativeNotification.getProps()) as T;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Renders the layout with new props and replaces the notification in place. The notification
|
|
50
|
+
* does not alert again when `alertOnlyOnce` is `true`.
|
|
51
|
+
*
|
|
52
|
+
* Rejects when the notification is no longer active, for example after the user dismissed it or
|
|
53
|
+
* after `cancel()`. Post it again in that case. Also rejects when the notification is canceled
|
|
54
|
+
* while the update renders, or for the same reasons as `post()`, such as an invalid `color`.
|
|
55
|
+
* @param props The new props for the layout function.
|
|
56
|
+
* @param options Presentation options to change. Options that are omitted keep their previous value.
|
|
57
|
+
* @platform android
|
|
58
|
+
*/
|
|
59
|
+
update(props: T, options: CustomNotificationUpdateOptions = {}): Promise<void> {
|
|
60
|
+
return this.nativeNotification.update(props as Record<string, any>, toNativeOptions(options));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Removes the notification from the shade immediately.
|
|
65
|
+
* Cancels any pending post or update for this notification.
|
|
66
|
+
* @platform android
|
|
67
|
+
*/
|
|
68
|
+
cancel(): Promise<void> {
|
|
69
|
+
return this.nativeNotification.cancel();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Posts and manages custom notifications that share one layout.
|
|
75
|
+
* @platform android
|
|
76
|
+
*/
|
|
77
|
+
export class CustomNotificationFactory<T extends object = object> {
|
|
78
|
+
/**
|
|
79
|
+
* The name passed to `createCustomNotification()`.
|
|
80
|
+
* @platform android
|
|
81
|
+
*/
|
|
82
|
+
readonly name: string;
|
|
83
|
+
/** @hidden */
|
|
84
|
+
private readonly nativeFactory: NativeCustomNotificationFactory | null;
|
|
85
|
+
|
|
86
|
+
/** @hidden */
|
|
87
|
+
constructor(name: string, layout: CustomNotificationComponent<T>) {
|
|
88
|
+
this.name = name;
|
|
89
|
+
this.nativeFactory =
|
|
90
|
+
Platform.OS === 'android'
|
|
91
|
+
? new ExpoWidgetsModule.CustomNotificationFactory(name, layout as unknown as string)
|
|
92
|
+
: null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Renders the layout and posts the notification. Posting with the `id` of an active notification
|
|
97
|
+
* replaces that notification in place.
|
|
98
|
+
*
|
|
99
|
+
* Rejects in these cases:
|
|
100
|
+
* - Notifications are disabled for the app.
|
|
101
|
+
* - `channelId` names a channel that does not exist.
|
|
102
|
+
* - `smallIcon` names a drawable that does not exist.
|
|
103
|
+
* - `color` is not in `#RRGGBB` or `#AARRGGBB` format.
|
|
104
|
+
* - The layout returns no `collapsed` section.
|
|
105
|
+
* - The post is canceled before rendering finishes.
|
|
106
|
+
* @param props The props for the layout function.
|
|
107
|
+
* @param options Presentation options.
|
|
108
|
+
* @returns A promise that resolves to a handle for the posted notification.
|
|
109
|
+
* @platform android
|
|
110
|
+
*/
|
|
111
|
+
async post(props: T, options: CustomNotificationOptions = {}): Promise<CustomNotification<T>> {
|
|
112
|
+
const nativeFactory = this.requireNativeFactory('post');
|
|
113
|
+
const id = options.id ?? uuid.v4();
|
|
114
|
+
const nativeNotification = await nativeFactory.post(
|
|
115
|
+
id,
|
|
116
|
+
props as Record<string, any>,
|
|
117
|
+
toNativeOptions({
|
|
118
|
+
...options,
|
|
119
|
+
ongoing: options.ongoing ?? false,
|
|
120
|
+
alertOnlyOnce: options.alertOnlyOnce ?? true,
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
return new CustomNotification<T>(nativeNotification);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Returns the active notification with the given key.
|
|
128
|
+
* @param id The key passed to or generated by `post()`.
|
|
129
|
+
* @returns A promise that resolves to the notification, or to `null` when no such notification is active.
|
|
130
|
+
* @platform android
|
|
131
|
+
*/
|
|
132
|
+
async get(id: string): Promise<CustomNotification<T> | null> {
|
|
133
|
+
const nativeNotification = await this.requireNativeFactory('get').get(id);
|
|
134
|
+
return nativeNotification ? new CustomNotification<T>(nativeNotification) : null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Returns every active notification posted with this layout, including notifications posted
|
|
139
|
+
* before a process restart.
|
|
140
|
+
* @platform android
|
|
141
|
+
*/
|
|
142
|
+
async getActive(): Promise<CustomNotification<T>[]> {
|
|
143
|
+
const nativeNotifications = await this.requireNativeFactory('getActive').getActive();
|
|
144
|
+
return nativeNotifications.map(
|
|
145
|
+
(nativeNotification) => new CustomNotification<T>(nativeNotification)
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Removes every active notification posted with this layout and cancels pending posts and updates.
|
|
151
|
+
* @platform android
|
|
152
|
+
*/
|
|
153
|
+
async cancelAll(): Promise<void> {
|
|
154
|
+
await this.requireNativeFactory('cancelAll').cancelAll();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private requireNativeFactory(methodName: string): NativeCustomNotificationFactory {
|
|
158
|
+
if (!this.nativeFactory) {
|
|
159
|
+
throw new UnavailabilityError('ExpoWidgets', `CustomNotificationFactory.${methodName}`);
|
|
160
|
+
}
|
|
161
|
+
return this.nativeFactory;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Creates a factory that posts notifications rendered from the given layout.
|
|
167
|
+
*
|
|
168
|
+
* The factory can be created on every platform. Its methods reject with `UnavailabilityError` on
|
|
169
|
+
* platforms other than Android.
|
|
170
|
+
* @param name A unique name for the layout. It identifies the layout when a notification button
|
|
171
|
+
* is pressed and when active notifications are recovered.
|
|
172
|
+
* @param layout The layout component, marked with the `'widget'` directive.
|
|
173
|
+
* @example
|
|
174
|
+
* ```tsx
|
|
175
|
+
* import { Row, Text } from '@expo/ui/jetpack-compose';
|
|
176
|
+
* import { createCustomNotification } from 'expo-widgets';
|
|
177
|
+
*
|
|
178
|
+
* type Props = { status: string };
|
|
179
|
+
*
|
|
180
|
+
* const OrderNotification = createCustomNotification('OrderNotification', (props: Props) => {
|
|
181
|
+
* 'widget';
|
|
182
|
+
* return {
|
|
183
|
+
* collapsed: (
|
|
184
|
+
* <Row>
|
|
185
|
+
* <Text>{props.status}</Text>
|
|
186
|
+
* </Row>
|
|
187
|
+
* ),
|
|
188
|
+
* };
|
|
189
|
+
* });
|
|
190
|
+
*
|
|
191
|
+
* const order = await OrderNotification.post({ status: 'Preparing' }, { id: 'order-1', ongoing: true });
|
|
192
|
+
* await order.update({ status: 'Delivered' }, { ongoing: false, timeoutAfter: 15 * 60 * 1000 });
|
|
193
|
+
* ```
|
|
194
|
+
* @platform android
|
|
195
|
+
*/
|
|
196
|
+
export function createCustomNotification<T extends object = object>(
|
|
197
|
+
name: string,
|
|
198
|
+
layout: CustomNotificationComponent<T>
|
|
199
|
+
): CustomNotificationFactory<T> {
|
|
200
|
+
return new CustomNotificationFactory<T>(name, layout);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Strip omitted values so native updates preserve the previous options.
|
|
204
|
+
function toNativeOptions(options: CustomNotificationOptions): CustomNotificationUpdateOptions {
|
|
205
|
+
const { id: _id, ongoing, autoCancel, ...rest } = options;
|
|
206
|
+
return Object.fromEntries(
|
|
207
|
+
Object.entries({
|
|
208
|
+
...rest,
|
|
209
|
+
ongoing,
|
|
210
|
+
autoCancel: autoCancel ?? (ongoing === undefined ? undefined : !ongoing),
|
|
211
|
+
}).filter(([, value]) => value !== undefined)
|
|
212
|
+
);
|
|
213
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { SharedObject } from 'expo';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The views of a custom notification. Each section is rendered into its own `RemoteViews` at a
|
|
6
|
+
* fixed size, so a section cannot depend on the size of another section.
|
|
7
|
+
* @platform android
|
|
8
|
+
*/
|
|
9
|
+
export type CustomNotificationLayout = {
|
|
10
|
+
/**
|
|
11
|
+
* The collapsed row shown in the notification shade. The system gives it about 64
|
|
12
|
+
* density-independent pixels (dp) of height.
|
|
13
|
+
*/
|
|
14
|
+
collapsed: ReactNode;
|
|
15
|
+
/**
|
|
16
|
+
* The expanded view shown when the user expands the notification. The system gives it up to
|
|
17
|
+
* 256 dp of height. When omitted, the system derives the expanded view from `collapsed`.
|
|
18
|
+
*/
|
|
19
|
+
expanded?: ReactNode;
|
|
20
|
+
/**
|
|
21
|
+
* The heads-up view shown when the notification peeks over the current screen. The system gives
|
|
22
|
+
* it about 88 dp of height. When omitted, the system reuses `collapsed`. The channel importance
|
|
23
|
+
* decides whether a heads-up view is shown at all.
|
|
24
|
+
*/
|
|
25
|
+
headsUp?: ReactNode;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Values the system provides to a custom notification layout.
|
|
30
|
+
* @platform android
|
|
31
|
+
*/
|
|
32
|
+
export type CustomNotificationEnvironment = {
|
|
33
|
+
/**
|
|
34
|
+
* The color scheme of the device at render time.
|
|
35
|
+
*/
|
|
36
|
+
colorScheme: 'light' | 'dark';
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A function that returns the views of a custom notification. It must start with the `'widget'`
|
|
41
|
+
* directive and use `@expo/ui/jetpack-compose` components.
|
|
42
|
+
* @platform android
|
|
43
|
+
*/
|
|
44
|
+
export type CustomNotificationComponent<T extends object = object> = (
|
|
45
|
+
props: T,
|
|
46
|
+
environment: CustomNotificationEnvironment
|
|
47
|
+
) => CustomNotificationLayout;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Presentation options of a custom notification.
|
|
51
|
+
* @platform android
|
|
52
|
+
*/
|
|
53
|
+
export type CustomNotificationOptions = {
|
|
54
|
+
/**
|
|
55
|
+
* The key of the notification. Posting with the key of an active notification replaces that
|
|
56
|
+
* notification in place. Defaults to a random UUID.
|
|
57
|
+
*/
|
|
58
|
+
id?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The notification channel the notification is posted to. The channel must exist, for example,
|
|
61
|
+
* one created with `expo-notifications`. Defaults to a low-importance channel with the ID
|
|
62
|
+
* `expo_widgets_notifications`, which `expo-widgets` creates on first use. The system settings
|
|
63
|
+
* list it as "Notifications".
|
|
64
|
+
*/
|
|
65
|
+
channelId?: string;
|
|
66
|
+
/**
|
|
67
|
+
* The content title. Screen readers announce it, and the system uses it when it cannot show the
|
|
68
|
+
* custom views.
|
|
69
|
+
*/
|
|
70
|
+
title?: string;
|
|
71
|
+
/**
|
|
72
|
+
* The content text. The system uses it when it cannot show the custom views.
|
|
73
|
+
*/
|
|
74
|
+
text?: string;
|
|
75
|
+
/**
|
|
76
|
+
* Whether the user cannot dismiss the notification with a swipe.
|
|
77
|
+
* @default false
|
|
78
|
+
*/
|
|
79
|
+
ongoing?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Whether a tap removes the notification. Defaults to the opposite of `ongoing`.
|
|
82
|
+
*/
|
|
83
|
+
autoCancel?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Whether sound, vibration, and the heads-up view are used only for the first post and not for
|
|
86
|
+
* updates.
|
|
87
|
+
* @default true
|
|
88
|
+
*/
|
|
89
|
+
alertOnlyOnce?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Time in milliseconds after which the system removes the notification. Useful after a final
|
|
92
|
+
* update that sets `ongoing` to `false`.
|
|
93
|
+
*/
|
|
94
|
+
timeoutAfter?: number;
|
|
95
|
+
/**
|
|
96
|
+
* A deep link opened in this app when the user taps the notification. Defaults to the launch
|
|
97
|
+
* intent of the app.
|
|
98
|
+
*/
|
|
99
|
+
url?: string;
|
|
100
|
+
/**
|
|
101
|
+
* The name of a drawable resource used as the small icon. Defaults to the icon configured by
|
|
102
|
+
* the `expo-notifications` config plugin, and then to the app icon.
|
|
103
|
+
*/
|
|
104
|
+
smallIcon?: string;
|
|
105
|
+
/**
|
|
106
|
+
* The accent color in `#RRGGBB` or `#AARRGGBB` format. Defaults to the color configured by the
|
|
107
|
+
* `expo-notifications` config plugin.
|
|
108
|
+
*/
|
|
109
|
+
color?: string;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Presentation options that an update can override. Options that an update omits keep the value
|
|
114
|
+
* from the previous post or update.
|
|
115
|
+
* @platform android
|
|
116
|
+
*/
|
|
117
|
+
export type CustomNotificationUpdateOptions = Omit<CustomNotificationOptions, 'id'>;
|
|
118
|
+
|
|
119
|
+
/** @hidden */
|
|
120
|
+
export declare class NativeCustomNotification extends SharedObject {
|
|
121
|
+
getId(): string;
|
|
122
|
+
getProps(): Promise<Record<string, any>>;
|
|
123
|
+
update(props: Record<string, any>, options: CustomNotificationUpdateOptions): Promise<void>;
|
|
124
|
+
cancel(): Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @hidden */
|
|
128
|
+
export declare class NativeCustomNotificationFactory extends SharedObject {
|
|
129
|
+
constructor(name: string, layout: string);
|
|
130
|
+
post(
|
|
131
|
+
id: string,
|
|
132
|
+
props: Record<string, any>,
|
|
133
|
+
options: CustomNotificationUpdateOptions
|
|
134
|
+
): Promise<NativeCustomNotification>;
|
|
135
|
+
get(id: string): Promise<NativeCustomNotification | null>;
|
|
136
|
+
getActive(): Promise<NativeCustomNotification[]>;
|
|
137
|
+
cancelAll(): Promise<void>;
|
|
138
|
+
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { NativeModule, requireNativeModule } from 'expo';
|
|
2
2
|
|
|
3
|
+
import type {
|
|
4
|
+
NativeCustomNotification,
|
|
5
|
+
NativeCustomNotificationFactory,
|
|
6
|
+
} from './CustomNotifications.types';
|
|
3
7
|
import type {
|
|
4
8
|
ExpoWidgetsEvents,
|
|
5
9
|
NativeLiveActivity,
|
|
@@ -11,8 +15,12 @@ declare class ExpoWidgetsModule extends NativeModule<ExpoWidgetsEvents> {
|
|
|
11
15
|
widgetsDirectory: string;
|
|
12
16
|
reloadAllWidgets(): void;
|
|
13
17
|
readonly Widget: typeof NativeWidgetObject;
|
|
18
|
+
/** iOS only. */
|
|
14
19
|
readonly LiveActivityFactory: typeof NativeLiveActivityFactory;
|
|
15
20
|
readonly LiveActivity: typeof NativeLiveActivity;
|
|
21
|
+
/** Android only. */
|
|
22
|
+
readonly CustomNotificationFactory: typeof NativeCustomNotificationFactory;
|
|
23
|
+
readonly CustomNotification: typeof NativeCustomNotification;
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
export default requireNativeModule<ExpoWidgetsModule>('ExpoWidgets');
|
package/src/ExpoWidgets.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { EventSubscription } from 'expo';
|
|
2
2
|
|
|
3
|
+
import type {
|
|
4
|
+
CustomNotificationUpdateOptions,
|
|
5
|
+
NativeCustomNotification,
|
|
6
|
+
NativeCustomNotificationFactory,
|
|
7
|
+
} from './CustomNotifications.types';
|
|
3
8
|
import type {
|
|
4
9
|
ExpoTimelineEntry,
|
|
5
10
|
ExpoWidgetsEvents,
|
|
@@ -56,12 +61,48 @@ class LiveActivityFactoryStub {
|
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
|
|
64
|
+
class CustomNotificationStub {
|
|
65
|
+
getId(): string {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
async getProps(): Promise<Record<string, any>> {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
async update(
|
|
72
|
+
_props: Record<string, any>,
|
|
73
|
+
_options: CustomNotificationUpdateOptions
|
|
74
|
+
): Promise<void> {}
|
|
75
|
+
async cancel(): Promise<void> {}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
class CustomNotificationFactoryStub {
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
80
|
+
constructor(_name: string, _layout: string) {}
|
|
81
|
+
async post(
|
|
82
|
+
_id: string,
|
|
83
|
+
_props: Record<string, any>,
|
|
84
|
+
_options: CustomNotificationUpdateOptions
|
|
85
|
+
): Promise<NativeCustomNotification> {
|
|
86
|
+
return new CustomNotificationStub() as unknown as NativeCustomNotification;
|
|
87
|
+
}
|
|
88
|
+
async get(_id: string): Promise<NativeCustomNotification | null> {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
async getActive(): Promise<NativeCustomNotification[]> {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
async cancelAll(): Promise<void> {}
|
|
95
|
+
}
|
|
96
|
+
|
|
59
97
|
const ExpoWidgetsModule = {
|
|
60
98
|
widgetsDirectory: '',
|
|
61
99
|
reloadAllWidgets(): void {},
|
|
62
100
|
Widget: WidgetStub as typeof NativeWidgetObject,
|
|
63
101
|
LiveActivityFactory: LiveActivityFactoryStub as typeof NativeLiveActivityFactory,
|
|
64
102
|
LiveActivity: LiveActivityStub as typeof NativeLiveActivity,
|
|
103
|
+
CustomNotificationFactory:
|
|
104
|
+
CustomNotificationFactoryStub as typeof NativeCustomNotificationFactory,
|
|
105
|
+
CustomNotification: CustomNotificationStub as typeof NativeCustomNotification,
|
|
65
106
|
addListener<EventName extends keyof ExpoWidgetsEvents>(
|
|
66
107
|
_eventName: EventName,
|
|
67
108
|
_listener: ExpoWidgetsEvents[EventName]
|