react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
753 stars 288 forks source link

event listener for 'localNotification' are not called in example App.js (or in my app) #440

Closed michelcrypt4d4mus closed 6 months ago

michelcrypt4d4mus commented 6 months ago

Have been trying to puzzle through this unsuccessfully. Seems like:

  1. sending and listening to notification (via DeviceEventEmitter like in example App.js) work fine
  2. sending a localNotification via addNotificationRequest() seems to work mostly ok - there's a console log message from the package [my_app] notifier request success and if i schedule the send in the future i will be able to see it returned when i call PushNotificationIOS.getPendingNotificationRequests().
  3. but the listener subscribed to localNotification events never fires.

Possibly unrelated but occasionally there is a slightly ominous console log message: [UIKitCore] Found no UIEvent for backing event of type: 3; contextId: 0x125C4048 but it doesn't happen all of the time (or even 25% of the time) when firing a localNotification.

Modeled a minimal reproducible sample based on the example App.js in this repo:

import PushNotificationIOS from '@react-native-community/push-notification-ios';
import React, { useEffect } from 'react';
import { Alert, StyleSheet, View } from 'react-native';

import Button from './src/app/components/button';

export default function App() {
    useEffect(() => {
        // Request permissions
        PushNotificationIOS.requestPermissions({
            alert: true,
            badge: true,
            sound: true,
            critical: false,  // Critical notifications require a higher level of permission from Apple
        }).then(
            data => console.log('Accepted PushNotificationIOS.requestPermissions', data),
            data => console.log('Rejected PushNotificationIOS notifications', data)
        );

        const onLocalNotification = (notification) => {
            console.log(`onLocalNotification() was called with ${JSON.stringify(notification)}...`);
            const isClicked = notification.getData().userInteraction === 1;

            Alert.alert(
                'Local Notification Received',
                `Alert title: ${notification.getTitle()}\n${JSON.stringify(notification)}\n`,
                [
                    {
                        text: 'Dismiss',
                        onPress: null,
                    },
                ],
            );
        };

        // add listener for localNotification events
        PushNotificationIOS.addEventListener('localNotification', onLocalNotification);
        // Also does not fire when listening for 'notification'
        // PushNotificationIOS.addEventListener('notification', onLocalNotification);
    }, []);

    const setNotificationCategories = async () => {
        PushNotificationIOS.setNotificationCategories([
            {
                id: 'userAction',
                actions: [
                    {
                        id: 'open',
                        title: 'Open',
                        options: { foreground: true }
                    },
                    {
                        id: 'ignore',
                        title: 'Desruptive',
                        options: { foreground: true, destructive: true },
                    },
                    {
                        id: 'text',
                        title: 'Text Input',
                        options: { foreground: true },
                        textInput: { buttonTitle: 'Send' },
                    },
                ],
            },
        ]);

        Alert.alert(
            'setNotificationCategories',
            `Set notification category complete`,
            [
                {
                    text: 'Dismiss',
                    onPress: null,
                },
            ],
        );
    };

    const addNotificationRequest = () => {
        msgID = `test_${Math.random()}`.slice(0, 9);

        PushNotificationIOS.addNotificationRequest({
            id: msgID,
            title: `TITLE: ${msgID}`,
            subtitle: 'subtitle',
            body: 'body',
            category: 'userAction',
            threadId: 'thread-id',
            fireDate: new Date(new Date().valueOf() + 2000),
        });
    };

    const getPendingNotificationRequests = () => {
        PushNotificationIOS.getPendingNotificationRequests((requests) => {
          Alert.alert('Push Notification Received', JSON.stringify(requests), [{text: 'Dismiss', onPress: null}]);
        });
    };

    return (
        <View style={styles.flex}>
            <Button onPress={setNotificationCategories} title="Set notification categories"/>
            <Button onPress={addNotificationRequest} title="Add notification request"/>
            <Button onPress={getPendingNotificationRequests} title="Get Pending Notification Requests"/>
        </View>
    );
};

const styles = StyleSheet.create({
    flex: {
        flex: 1,
        padding: 120,
    },
    button: {
        margin: 20,
    }
});

Console Logs From Pushing The Above Buttons:

 LOG  Adding listener for localNotification events...
 LOG  Accepted PushNotificationIOS.requestPermissions {"alert": true, "authorizationStatus": 2, "badge": true, "critical": false, "lockScreen": true, "notificationCenter": true, "sound": true}
[canarycastle] notifier request success
[UIKitCore] Found no UIEvent for backing event of type: 3; contextId: 0x110CD40A
[canarycastle] notifier request success
[canarycastle] notifier request success
[canarycastle] notifier request success
[canarycastle] notifier request success
[canarycastle] notifier request success
[canarycastle] notifier request success
[canarycastle] notifier request success
[UIKitCore] Found no UIEvent for backing event of type: 3; contextId: 0x87FE0E7F
michelcrypt4d4mus commented 6 months ago

i think this is related to trying to run the app in expo which requires different changes to AppDelegate.mm and AppDelegate.h. Closing for now.