diff --git a/example/payload.json b/example/payload.json index d917a72..641dd71 100644 --- a/example/payload.json +++ b/example/payload.json @@ -6,7 +6,7 @@ }, "content-available": 1 }, - "custom": { + "data": { "key1": "value1", "key2": "value2" } diff --git a/example/src/App.tsx b/example/src/App.tsx index 01b6d36..f499e72 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -30,6 +30,9 @@ export default function App() { } } ); + Push.addErrorListener(NativeEvent.FAILED_TO_REGISTER, (message) => { + console.log('FAILED_TO_REGISTER:', message); + }); Push.addMessageEventListener(NativeEvent.NOTIFICATION_OPENED, (message) => { console.log('NOTIFICATION_OPENED:', message); }); diff --git a/ios/Push.swift b/ios/Push.swift index b9a831f..910feaa 100644 --- a/ios/Push.swift +++ b/ios/Push.swift @@ -6,7 +6,8 @@ private let expectedEventNames: Set = [ NativeEvent.backgroundMessageReceived.name, NativeEvent.foregroundMessageReceived.name, NativeEvent.notificationOpened.name, - NativeEvent.launchNotificationOpened.name + NativeEvent.launchNotificationOpened.name, + NativeEvent.failedToRegister.name ] @objc(Push) @@ -120,6 +121,7 @@ final class Push: RCTEventEmitter { NativeEvent.notificationOpened.key: NativeEvent.notificationOpened.name, NativeEvent.launchNotificationOpened.key: NativeEvent.launchNotificationOpened.name, NativeEvent.tokenReceived.key: NativeEvent.tokenReceived.name, + NativeEvent.failedToRegister.key: NativeEvent.failedToRegister.name, ], ] } diff --git a/ios/PushHelper.swift b/ios/PushHelper.swift index ce338ed..daeb6df 100644 --- a/ios/PushHelper.swift +++ b/ios/PushHelper.swift @@ -23,6 +23,7 @@ extension UNAuthorizationStatus { enum NativeEvent { case tokenReceived + case failedToRegister case notificationOpened case launchNotificationOpened case backgroundMessageReceived @@ -40,6 +41,8 @@ enum NativeEvent { return "BACKGROUND_MESSAGE_RECEIVED" case .foregroundMessageReceived: return "FOREGROUND_MESSAGE_RECEIVED" + case .failedToRegister: + return "FAILED_TO_REGISTER" } } @@ -55,6 +58,8 @@ enum NativeEvent { return "ForegroundMessageReceived" case .backgroundMessageReceived: return "BackgroundMessageReceived" + case .failedToRegister: + return "FailedToRegister" } } } @@ -224,7 +229,9 @@ final class PushNotificationManager { } func didFailToRegisterForRemoteNotificationsWithError(error: Error) { - print("Register for remote notifications failed due to \(error).") + sharedEventManager.sendEventToJS( + PushEvent(type: NativeEvent.failedToRegister, payload: ["message": error.localizedDescription]) + ) } func didReceiveRemoteNotification( @@ -321,7 +328,6 @@ final class PushNotificationManager { @objc private func applicationDidBecomeActive() { -// registerForRemoteNotifications() } @objc diff --git a/src/apis/addErrorListener.ts b/src/apis/addErrorListener.ts new file mode 100644 index 0000000..f2c9ba0 --- /dev/null +++ b/src/apis/addErrorListener.ts @@ -0,0 +1,11 @@ +import type { EmitterSubscription } from 'react-native'; + +import { nativeEventEmitter } from '../nativeModule'; + +export const addErrorListener = ( + event: string, + listener: (message: string) => void +): EmitterSubscription => + nativeEventEmitter.addListener(event, ({ message }: { message: string }) => { + listener(message); + }); diff --git a/src/apis/index.ts b/src/apis/index.ts index b279a5e..e2d2f0c 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -10,3 +10,4 @@ export { requestPermissions } from './requestPermissions'; export { setBadgeCount } from './setBadgeCount'; export { registerForToken } from './registerForToken'; export { removeListeners } from './removeListeners'; +export { addErrorListener } from './addErrorListener'; diff --git a/src/index.ts b/src/index.ts index 2160718..eec2e5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { addMessageEventListener, addTokenEventListener, + addErrorListener, completeNotification, getBadgeCount, getConstants, @@ -32,6 +33,7 @@ const module = { setBadgeCount, registerForToken, removeListeners, + addErrorListener, }; export type PushNotificationModule = typeof module; diff --git a/src/types/native.ts b/src/types/native.ts index e9f793a..2378cd8 100644 --- a/src/types/native.ts +++ b/src/types/native.ts @@ -14,6 +14,7 @@ export interface PushNotificationNativeModule extends NativeModule { LAUNCH_NOTIFICATION_OPENED: string; NOTIFICATION_OPENED: string; TOKEN_RECEIVED: string; + FAILED_TO_REGISTER: string; }; NativeHeadlessTaskKey?: string; };