Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification is not triggering inside the setBackgroundMessageHandler and not onBackground event is triggering. #1131

Closed
Awais7899 opened this issue Oct 24, 2024 · 1 comment

Comments

@Awais7899
Copy link

Awais7899 commented Oct 24, 2024

index .js

/**

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';
import notifee, {AndroidImportance, AuthorizationStatus} from '@notifee/react-native';

/**

  • Check and request notification permission (for Android 13+)
    */
    async function checkNotificationPermission() {
    try {
    const settings = await notifee.getNotificationSettings();
    console.log('Notification Settings:', settings);
    console.log(AuthorizationStatus.DENIED)

    if (settings.authorizationStatus === AuthorizationStatus.DENIED) {
    console.log('Requesting permission for notifications...');
    const newSettings = await notifee.requestPermission();
    console.log('New Notification Settings:', newSettings);
    }
    } catch (error) {
    console.log('Error checking/requesting notification permissions:', error);
    }
    }

/**

  • Foreground Service Registration
    */
    notifee.registerForegroundService(notification => {
    return new Promise(() => {
    // Do nothing for now
    });
    });

/**

  • Background Event Handling for Notifee
    */
    notifee.onBackgroundEvent(async ({type, detail}) => {
    console.log('Background event received:', type, detail);

if (type === notifee.EventType.DISMISSED || type === notifee.EventType.PRESS) {
// Handle event, e.g., dismiss call or navigate
console.log('Notification event handled:', type);
}
});

let notificationId; // Variable to hold the notification ID

/**

  • Firebase Background Message Handler
    */
    messaging().setBackgroundMessageHandler(async remoteMessage => {
    try {
    console.log('Remote message received in background:', remoteMessage);

    // Parse the message data to extract the caller details
    const user = JSON.parse(remoteMessage?.data?.user);
    const callerName = ${user?.firstName} ${user?.lastName};

    // Create a notification channel for video calls
    const channelId = await notifee.createChannel({
    id: 'video_call',
    name: 'Video Call',
    importance: AndroidImportance.HIGH,
    });
    console.log('Channel created with ID:', channelId);

    // Display the incoming call notification
    notificationId = await notifee.displayNotification({
    title: 'Incoming call',
    body: Call is coming from ${callerName},
    android: {
    channelId,
    sound: 'default',
    importance: AndroidImportance.HIGH,
    visibility: AndroidVisibility.PUBLIC,
    importance: AndroidImportance.HIGH,
    category: AndroidCategory.MESSAGE,
    pressAction: {id: 'default'}, // Set an action for pressing the notification
    },
    });
    console.log('Notification displayed with ID:', notificationId);
    } catch (error) {
    console.error('Error displaying notification:', error);
    }
    });

/**

  • Register the app component
    */
    AppRegistry.registerComponent(appName, () => App);

/**

  • Initialize notification permission check on app start
    */
    checkNotificationPermission();

    "react-native": "0.75.4",
    "@notifee/react-native": "^9.1.2",

@LunatiqueCoder
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants