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

Commit

Permalink
fix: api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 17, 2024
1 parent 93cc967 commit 5508da7
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ios": "react-native run-ios",
"start": "react-native start",
"build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
"push": "xcrun simctl push booted com.pushexample.app payload.json",
"push": "xcrun simctl push booted com.pushexample2.app payload.json",
"build:ios": "cd ios && xcodebuild -workspace PushExample.xcworkspace -scheme PushExample -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO"
},
"dependencies": {
Expand Down
67 changes: 65 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,83 @@ import { StyleSheet, View, Text, Button } from 'react-native';
import type { PushNotificationPermissionStatus } from '@candlefinance/push';
import { module as Push } from '@candlefinance/push';

Push.registerHeadlessTask(async (message) => {
console.log('Headless Task', message);
});

export default function App() {
const [permissionStatus, setPermissionStatus] = React.useState<
PushNotificationPermissionStatus | undefined
>(undefined);
const [isGranted, setIsGranted] = React.useState<boolean>(false);

React.useEffect(() => {
const { NativeEvent, NativeHeadlessTaskKey } = Push.getConstants();
console.log(NativeEvent, NativeHeadlessTaskKey);
Push.addTokenEventListener(NativeEvent.TOKEN_RECEIVED, (token) => {
console.log('TOKEN_RECEIVED:', token);
});
Push.addMessageEventListener(
NativeEvent.BACKGROUND_MESSAGE_RECEIVED,
(message, id) => {
console.log('BACKGROUND_MESSAGE_RECEIVED:', message);
if (id !== undefined) {
console.log('Completing notification:', id);
Push.completeNotification(id);
}
}
);
Push.addMessageEventListener(NativeEvent.NOTIFICATION_OPENED, (message) => {
console.log('NOTIFICATION_OPENED:', message);
});
Push.addMessageEventListener(
NativeEvent.FOREGROUND_MESSAGE_RECEIVED,
(message) => {
console.log('FOREGROUND_MESSAGE_RECEIVED:', message);
}
);
Push.addMessageEventListener(
NativeEvent.LAUNCH_NOTIFICATION_OPENED,
(message) => {
console.log('LAUNCH_NOTIFICATION_OPENED:', message);
}
);
return () => {
Push.removeListeners(NativeEvent.TOKEN_RECEIVED);
Push.removeListeners(NativeEvent.BACKGROUND_MESSAGE_RECEIVED);
Push.removeListeners(NativeEvent.NOTIFICATION_OPENED);
Push.removeListeners(NativeEvent.FOREGROUND_MESSAGE_RECEIVED);
Push.removeListeners(NativeEvent.LAUNCH_NOTIFICATION_OPENED);
};
}, [isGranted]);

return (
<View style={styles.container}>
<Text style={styles.text}>Authorization Status</Text>
<Text style={styles.text}>Authorization Status: {permissionStatus}</Text>
<Text style={styles.text}>isGranted: {isGranted}</Text>
<Button
title="Request Permissions"
title="Get Status"
onPress={() => {
Push.getPermissionStatus().then(
(status: PushNotificationPermissionStatus) => {
console.log(status);
setPermissionStatus(status);
}
);
}}
/>
<Button
title="Request Permissions"
onPress={() => {
Push.requestPermissions().then((granted) => {
console.log(granted);
setIsGranted(granted);
if (granted) {
Push.registerForToken();
}
});
}}
/>
</View>
);
}
Expand Down
2 changes: 1 addition & 1 deletion ios/PushHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ final class PushNotificationManager {

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

@objc
Expand Down
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { registerHeadlessTask } from './registerHeadlessTask';
export { requestPermissions } from './requestPermissions';
export { setBadgeCount } from './setBadgeCount';
export { registerForToken } from './registerForToken';
export { removeListeners } from './removeListeners';
5 changes: 5 additions & 0 deletions src/apis/removeListeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { nativeEventEmitter } from '../nativeModule';

export const removeListeners = (event: string) => {
nativeEventEmitter.removeAllListeners(event);
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
requestPermissions,
setBadgeCount,
registerForToken,
removeListeners,
} from './apis';

export type {
Expand All @@ -30,6 +31,7 @@ const module = {
requestPermissions,
setBadgeCount,
registerForToken,
removeListeners,
};

export type PushNotificationModule = typeof module;
Expand Down
4 changes: 2 additions & 2 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export interface PushNotificationMessage {
fcmOptions?: FcmPlatformOptions;
apnsOptions?: ApnsPlatformOptions;
data?: Record<string, unknown>;
custom?: any;
}

export type PushNotificationPermissionStatus =
| 'denied'
| 'granted'
| 'shouldRequest'
| 'shouldExplainThenRequest';
| 'notDetermined';

interface ApnsPlatformOptions {
subtitle?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface PushNotificationNativeModule extends NativeModule {
completeNotification?(completionHandlerId: string): void;
getConstants(): {
NativeEvent: {
BACKGROUND_MESSAGE_RECEIVED?: string;
BACKGROUND_MESSAGE_RECEIVED: string;
FOREGROUND_MESSAGE_RECEIVED: string;
LAUNCH_NOTIFICATION_OPENED: string;
NOTIFICATION_OPENED: string;
Expand Down
5 changes: 3 additions & 2 deletions src/utils/normalizeNativeMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ export const normalizeNativeMessage = (
} else {
return null;
}
const { body, imageUrl, title, options, data } = normalized;
const { body, imageUrl, title, options, data, custom } = normalized;

return {
body,
data,
imageUrl,
title,
custom,
...options,
};
};
Expand All @@ -43,7 +44,7 @@ const normalizeApnsMessage = (apnsMessage: ApnsMessage): NormalizedValues => {
const { body, title } = aps.alert ?? {};
const options = getApnsOptions(apnsMessage);

return { body, title, options, data, custom };
return { body, title, options, data, custom: custom };
};

const normalizeFcmMessage = (fcmMessage: FcmMessage): NormalizedValues => {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/normalizeNativePermissionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ export const normalizeNativePermissionStatus = (
): PushNotificationPermissionStatus => {
switch (nativeStatus) {
case 'ShouldRequest':
return 'shouldRequest';
case 'NotDetermined':
case 'ShouldExplainThenRequest':
return 'shouldExplainThenRequest';
return 'notDetermined';
case 'Authorized':
case 'Granted':
return 'granted';
Expand Down

0 comments on commit 5508da7

Please sign in to comment.