-
Notifications
You must be signed in to change notification settings - Fork 261
Description
index .js
/**
- @Format
*/
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",