-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (63 loc) · 2.29 KB
/
index.js
File metadata and controls
71 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @format
*/
import {AppRegistry, AppState} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';
import notifee, { AndroidImportance,EventType } from '@notifee/react-native';
// Create a channel
async function createNotificationChannel() {
return await notifee.createChannel({
id: 'default',
name: 'Default',
vibration:true,
vibrationPattern:[300,500],
importance:AndroidImportance.HIGH
});
}
// Display a notification
async function displayNotification(title,body) {
await notifee.requestPermission();
const channelId=await createNotificationChannel();
console.info(channelId);
await notifee.displayNotification({
title:title,
body:body,
android: {
channelId: channelId,
importance:AndroidImportance.HIGH,
pressAction:{
id:'default'
},
sound: 'default',
smallIcon: 'ic_launcher', // Make sure to add an icon to your project
},
});
}
// Firebase background message handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Background Notification:', remoteMessage);
if(remoteMessage.data && AppState.currentState!=='active'){
// if(remoteMessage.notification && AppState.currentState!=='active'){
//if notification tag then (default will run and you can set this one as well)
// displayNotification(remoteMessage.notification?.title, remoteMessage.notification?.body + ' ,background');
displayNotification(remoteMessage.data?.title, remoteMessage.data?.body + ' ,background');
}
});
messaging().getInitialNotification().then(async(remoteMessage)=>{
if(remoteMessage){
console.log("getting initial notification");
}
})
// Notifee background event listener
notifee.onBackgroundEvent(async ({ type, detail }) => {
if(type===EventType.DISMISSED){
console.log("notification dismissed background listener");
}
if (type === EventType.PRESS) {
console.log('Notification pressed in background:', detail.notification);
// Handle background notification press (e.g., deep linking or navigation)
}
});
AppRegistry.registerComponent(appName, () => App);