diff --git a/example/package.json b/example/package.json index 068f329..6879d8f 100644 --- a/example/package.json +++ b/example/package.json @@ -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": { diff --git a/example/src/App.tsx b/example/src/App.tsx index b0978a8..01b6d36 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -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(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 ( - Authorization Status + Authorization Status: {permissionStatus} + isGranted: {isGranted}