import React, {useState, useEffect} from 'react'; import { StyleSheet, View, Text, Button, Platform, } from 'react-native'; import { Notifications, NotificationAction, NotificationCategory, NotificationBackgroundFetchResult, Notification, } from '../lib/src'; export default function NotificationsExampleApp() { const [notifications, setNotifications] = useState([]); const [openedNotifications, setOpenedNotifications] = useState([]); useEffect(() => { registerNotificationEvents(); setCategories(); getInitialNotification(); }, []) const registerNotificationEvents = () => { Notifications.events().registerNotificationReceivedForeground((notification, completion) => { setNotifications([...notifications, notification]); completion({alert: notification.payload.showAlert, sound: false, badge: false}); }); Notifications.events().registerNotificationOpened((notification, completion) => { setOpenedNotifications([notification, ...openedNotifications]); completion(); }); Notifications.events().registerNotificationReceivedBackground((notification, completion) => { completion(NotificationBackgroundFetchResult.NO_DATA); }); if (Platform.OS === 'ios') { Notifications.ios.events().appNotificationSettingsLinked(() => { console.warn('App Notification Settings Linked') }); } } const requestPermissionsIos = (options) => { Notifications.ios.registerRemoteNotifications( Object.fromEntries(options.map(opt => [opt, true])) ); } const requestPermissions = () => { Notifications.registerRemoteNotifications(); } const setCategories = () => { const upvoteAction = new NotificationAction( 'UPVOTE_ACTION', 'background', String.fromCodePoint(0x1F44D), false, ); const replyAction = new NotificationAction( 'REPLY_ACTION', 'background', 'Reply', true, { buttonTitle: 'Reply now', placeholder: 'Insert message' }, ); const category = new NotificationCategory( 'SOME_CATEGORY', [upvoteAction, replyAction] ); Notifications.setCategories([category]); } const sendLocalNotification = () => { Notifications.postLocalNotification({ identifier: '0', body: 'Local notification!', title: 'Local Notification Title', sound: 'chime.aiff', badge: 0, type: '', thread: '', payload: { category: 'SOME_CATEGORY', link: 'localNotificationLink', android_channel_id: 'my-channel', } }); } const removeAllDeliveredNotifications = () => { Notifications.removeAllDeliveredNotifications(); } const setNotificationChannel = () => { Notifications.setNotificationChannel({ channelId: 'my-channel', name: 'My Channel', groupId: 'my-group-id', groupName: 'my group name', importance: 5, description: 'My Description', enableLights: true, enableVibration: true, showBadge: true, soundFile: 'doorbell.mp3', vibrationPattern: [200, 1000, 500, 1000, 500], }) } const getInitialNotification = async () => { const initialNotification = await Notifications.getInitialNotification(); if (initialNotification) { setNotifications([initialNotification, ...notifications]); } } const renderNotification = (notification) => { return ( {`Title: ${notification.title}`} {`Body: ${notification.body}`} {`Extra Link Param: ${notification.payload.link}`} ); } const renderOpenedNotification = (notification) => { return ( {`Title: ${notification.title}`} {`Body: ${notification.body}`} {`Notification Clicked: ${notification.payload.link}`} ); } const checkPermissions = () => { Notifications.ios.checkPermissions().then((currentPermissions) => { console.warn(currentPermissions); }); } const isRegistered = () => { Notifications.isRegisteredForRemoteNotifications().then((registered) => { console.warn(registered); }); } return (