Skip to content

Commit 88bf9f7

Browse files
committed
feat: allow non promise on setNotificationHandler
1 parent e5f0200 commit 88bf9f7

8 files changed

Lines changed: 859 additions & 676 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { reactNativeConfigs } from '@goatjs/react-native-eslint';
22
import { defineConfig, globalIgnores } from '@eslint/config-helpers';
33

44
export default defineConfig(
5-
globalIgnores(['.yarn/**', 'android/**', 'ios/**', 'lib']),
5+
globalIgnores(['.yarn/**', 'android/**', 'ios/**', 'lib', 'example/.maestro']),
66
...reactNativeConfigs({ tsconfigRootDir: import.meta.dirname }),
77
{
88
rules: {

example/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
},
1717
"dependencies": {
1818
"@react-native-clipboard/clipboard": "^1.16.3",
19-
"@react-navigation/native": "^7.3.12",
20-
"@react-navigation/native-stack": "^7.18.4",
19+
"@react-navigation/native": "^7.3.16",
20+
"@react-navigation/native-stack": "^7.18.8",
2121
"react": "19.2.3",
22-
"react-native": "0.86.0",
23-
"react-native-nitro-modules": "^0.36.1",
24-
"react-native-safe-area-context": "^5.8.0",
25-
"react-native-screens": "^4.26.2"
22+
"react-native": "0.86.2",
23+
"react-native-nitro-modules": "^0.36.5",
24+
"react-native-safe-area-context": "^5.9.0",
25+
"react-native-screens": "^4.27.0"
2626
},
2727
"devDependencies": {
2828
"@babel/core": "^7.29.7",
@@ -31,13 +31,13 @@
3131
"@react-native-community/cli": "20.2.0",
3232
"@react-native-community/cli-platform-android": "20.2.0",
3333
"@react-native-community/cli-platform-ios": "20.2.0",
34-
"@react-native/babel-preset": "0.86.0",
35-
"@react-native/jest-preset": "0.86.0",
34+
"@react-native/babel-preset": "0.86.2",
35+
"@react-native/jest-preset": "0.86.2",
3636
"@react-native/metro-config": "0.86.0",
37-
"@react-native/typescript-config": "0.86.0",
38-
"@types/react": "^19.2.17",
37+
"@react-native/typescript-config": "0.86.2",
38+
"@types/react": "^19.2.18",
3939
"babel-plugin-react-compiler": "1.0.0",
40-
"execa": "^10.0.0",
40+
"execa": "^10.0.1",
4141
"react-native-builder-bob": "^0.43.0",
4242
"react-native-monorepo-config": "^0.4.0"
4343
},

example/src/context/NotificationContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
/* eslint-disable react-doctor/jsx-no-new-object-as-prop */
12
import type { ReactNode } from 'react';
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import { type PermissionStatus, type NotificationPresentationOptions, Notifications } from 'react-native-nitro-notification';
25
import { createContext, use, useEffect, useRef, useState } from 'react';
36
import { Alert } from 'react-native';
4-
import { type PermissionStatus, type NotificationPresentationOptions, Notifications } from 'react-native-nitro-notification';
57

68
interface NotificationContextValue {
79
permStatus: PermissionStatus;
@@ -26,9 +28,8 @@ export const NotificationProvider = ({ children }: { readonly children: ReactNod
2628
const [token, setToken] = useState<string>();
2729
const [lastReceived, setLastReceived] = useState<string>();
2830
const [presentationOptions, setPresentationOptions] = useState<NotificationPresentationOptions>(DEFAULT_PRESENTATION_OPTIONS);
29-
30-
// Ref so the handler always reads the latest options without being re-registered.
3131
const presentationOptionsRef = useRef(presentationOptions);
32+
3233
useEffect(() => {
3334
presentationOptionsRef.current = presentationOptions;
3435
}, [presentationOptions]);
@@ -71,7 +72,7 @@ export const NotificationProvider = ({ children }: { readonly children: ReactNod
7172
useEffect(() => {
7273
Notifications.setNotificationHandler((n) => {
7374
setLastReceived(`Received: ${n.title ?? ''}${n.body ?? ''}`);
74-
return Promise.resolve(presentationOptionsRef.current);
75+
return presentationOptionsRef.current;
7576
});
7677
return () => {
7778
Notifications.setNotificationHandler(undefined);

ios/HybridNitroNotification.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import UserNotifications
44
import NitroModules
55

66
final class HybridNitroNotification: HybridNitroNotificationSpec {
7-
8-
// MARK: - Permissions
9-
107
func requestPermissions(options: RequestPermissionsOptions?) throws -> Promise<PermissionStatus> {
118
return Promise.async {
129
let center = UNUserNotificationCenter.current()

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,33 @@
6565
"update": "yarn upgrade-interactive"
6666
},
6767
"devDependencies": {
68-
"@eslint/config-helpers": "^0.6.0",
69-
"@goatjs/dbz": "^2.62.0",
70-
"@goatjs/react-native-eslint": "^3.8.0",
68+
"@eslint/config-helpers": "^0.7.0",
69+
"@goatjs/dbz": "^2.64.0",
70+
"@goatjs/react-native-eslint": "^3.20.0",
7171
"@goatjs/typescript-config": "^2.2.0",
72-
"@react-native/babel-preset": "0.86.0",
73-
"@react-native/eslint-config": "0.86.0",
74-
"@types/node": "^26.1.1",
75-
"@types/react": "^19.2.17",
72+
"@react-native/babel-preset": "0.86.2",
73+
"@react-native/eslint-config": "0.86.2",
74+
"@types/node": "^26.2.0",
75+
"@types/react": "^19.2.18",
7676
"babel-plugin-react-compiler": "1.0.0",
7777
"del-cli": "^7.0.0",
78-
"eslint": "^10.7.0",
79-
"execa": "^10.0.0",
80-
"nitrogen": "^0.36.1",
81-
"prettier": "^3.9.5",
78+
"eslint": "^10.8.1",
79+
"execa": "^10.0.1",
80+
"nitrogen": "^0.36.5",
81+
"prettier": "^3.9.6",
8282
"react": "19.2.3",
83-
"react-native": "0.86.0",
83+
"react-native": "0.86.2",
8484
"react-native-builder-bob": "^0.43.0",
85-
"react-native-nitro-modules": "^0.36.1",
86-
"turbo": "^2.10.5",
85+
"react-native-nitro-modules": "^0.36.5",
86+
"turbo": "^2.10.9",
8787
"typescript": "^6.0.3"
8888
},
8989
"peerDependencies": {
9090
"react": "*",
9191
"react-native": "*",
9292
"react-native-nitro-modules": "*"
9393
},
94-
"packageManager": "yarn@4.17.1+sha512.ccbfabf7d7b6b32075088be9386fb9a2e00bb6887ef07fa56effabc890a56d53da1ccc4128d62db245fcbd3961b236d75335bdf7d5320ed6eafb7588b7ad4697",
94+
"packageManager": "yarn@4.18.0+sha512.fcb8716fe7cd0eece141ffc18b92193a9df9204c1ba83189c288835223fc0bbe64af473bab0d5e9927a7daeb5caf2bb07eb2787cc9338ca040ea125f2a1f2f7e",
9595
"publishConfig": {
9696
"registry": "https://registry.npmjs.org/"
9797
},

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { NitroNotification } from './NitroNotification.nitro';
2+
import type { NotificationHandler } from './types/NotificationHandler';
3+
import type { NotificationPayload } from './types/NotificationPayload';
24
import { NitroModules } from 'react-native-nitro-modules';
35
import { createNotificationListeners } from './NotificationListeners';
46

@@ -11,7 +13,10 @@ export const Notifications = {
1113
getPermissionStatus: native.getPermissionStatus.bind(native),
1214
getDevicePushToken: native.getDevicePushToken.bind(native),
1315
unregisterForNotifications: native.unregisterForNotifications.bind(native),
14-
setNotificationHandler: native.setNotificationHandler.bind(native),
16+
setNotificationHandler: (handler: NotificationHandler | undefined, handlerTimeoutMs?: number): void => {
17+
const wrappedHandler = handler === undefined ? undefined : (n: NotificationPayload) => Promise.resolve(handler(n));
18+
native.setNotificationHandler(wrappedHandler, handlerTimeoutMs);
19+
},
1520
addOnTokenRefreshed: listeners.addOnTokenRefreshed,
1621
addOnNotificationTapped: listeners.addOnNotificationTapped,
1722
getBadgeCount: native.getBadgeCount.bind(native),

src/types/NotificationHandler.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import type { NotificationPayload } from './NotificationPayload';
22
import type { NotificationPresentationOptions } from './NotificationPresentationOptions';
33

4-
export type NotificationHandler = (
5-
notification: NotificationPayload
6-
) => Promise<NotificationPresentationOptions>;
4+
export type NotificationHandler = (notification: NotificationPayload) => NotificationPresentationOptions | Promise<NotificationPresentationOptions>;

0 commit comments

Comments
 (0)