Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
fix: add registration failure listener
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 17, 2024
1 parent 5508da7 commit c83d1b7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/payload.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"content-available": 1
},
"custom": {
"data": {
"key1": "value1",
"key2": "value2"
}
Expand Down
3 changes: 3 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 3 additions & 1 deletion ios/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ private let expectedEventNames: Set<String> = [
NativeEvent.backgroundMessageReceived.name,
NativeEvent.foregroundMessageReceived.name,
NativeEvent.notificationOpened.name,
NativeEvent.launchNotificationOpened.name
NativeEvent.launchNotificationOpened.name,
NativeEvent.failedToRegister.name
]

@objc(Push)
Expand Down Expand Up @@ -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,
],
]
}
Expand Down
10 changes: 8 additions & 2 deletions ios/PushHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension UNAuthorizationStatus {

enum NativeEvent {
case tokenReceived
case failedToRegister
case notificationOpened
case launchNotificationOpened
case backgroundMessageReceived
Expand All @@ -40,6 +41,8 @@ enum NativeEvent {
return "BACKGROUND_MESSAGE_RECEIVED"
case .foregroundMessageReceived:
return "FOREGROUND_MESSAGE_RECEIVED"
case .failedToRegister:
return "FAILED_TO_REGISTER"
}
}

Expand All @@ -55,6 +58,8 @@ enum NativeEvent {
return "ForegroundMessageReceived"
case .backgroundMessageReceived:
return "BackgroundMessageReceived"
case .failedToRegister:
return "FailedToRegister"
}
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -321,7 +328,6 @@ final class PushNotificationManager {

@objc
private func applicationDidBecomeActive() {
// registerForRemoteNotifications()
}

@objc
Expand Down
11 changes: 11 additions & 0 deletions src/apis/addErrorListener.ts
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { requestPermissions } from './requestPermissions';
export { setBadgeCount } from './setBadgeCount';
export { registerForToken } from './registerForToken';
export { removeListeners } from './removeListeners';
export { addErrorListener } from './addErrorListener';
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addMessageEventListener,
addTokenEventListener,
addErrorListener,
completeNotification,
getBadgeCount,
getConstants,
Expand Down Expand Up @@ -32,6 +33,7 @@ const module = {
setBadgeCount,
registerForToken,
removeListeners,
addErrorListener,
};

export type PushNotificationModule = typeof module;
Expand Down
1 change: 1 addition & 0 deletions src/types/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit c83d1b7

Please sign in to comment.