-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePushNotifications.js
More file actions
149 lines (132 loc) · 4.24 KB
/
usePushNotifications.js
File metadata and controls
149 lines (132 loc) · 4.24 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React from 'react';
import messaging from '@react-native-firebase/messaging';
import {PermissionsAndroid, Platform} from 'react-native';
import notifee, {TriggerType} from '@notifee/react-native';
const usePushNotification = () => {
const requestUserPermission = async () => {
if (Platform.OS === 'ios') {
//Request iOS permission
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
} else if (Platform.OS === 'android') {
//Request Android permission (For API level 33+, for 32 or below is not required)
const res = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
}
};
const getFCMToken = async () => {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log('Your Firebase Token is:', fcmToken);
} else {
console.log('Failed', 'No token received');
}
};
const listenToForegroundNotifications = async () => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log(
'A new message arrived! (FOREGROUND)',
JSON.stringify(remoteMessage),
);
console.log('antes de mostrar');
// Request permissions (required for iOS)
await notifee.requestPermission();
// Create a channel (required for Android)
const channelId = await notifee.createChannel({
id: 'sound',
name: 'Default Channel',
});
// Display a notification
await notifee.displayNotification({
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
android: {
channelId,
},
});
console.log('después de mostrar');
console.log(
'🚀 ~ file: usePushNotifications.js:44 ~ unsubscribe ~ remoteMessage.notification.title:',
remoteMessage.notification.title,
);
});
return unsubscribe;
};
const listenToBackgroundNotifications = async () => {
const unsubscribe = messaging().setBackgroundMessageHandler(
async remoteMessage => {
console.log(
'A new message arrived! (BACKGROUND)',
JSON.stringify(remoteMessage),
);
},
);
return unsubscribe;
};
const onNotificationOpenedAppFromBackground = async () => {
const unsubscribe = messaging().onNotificationOpenedApp(
async remoteMessage => {
console.log(
'App opened from BACKGROUND by tapping notification:',
JSON.stringify(remoteMessage),
);
},
);
return unsubscribe;
};
const onNotificationOpenedAppFromQuit = async () => {
const message = await messaging().getInitialNotification();
if (message) {
console.log(
'App opened from QUIT by tapping notification:',
JSON.stringify(message),
);
}
};
const scheduleNotification = async (dateString, title, message) => {
// Request permissions (required for iOS)
await notifee.requestPermission();
// Create a channel (required for Android)
const channelId = await notifee.createChannel({
id: 'sound',
name: 'Default Channel',
});
const date = new Date(dateString); // Suma 60000 milisegundos (1 minuto) al tiempo actual
console.log(
'🚀 ~ file: usePushNotifications.js:106 ~ scheduleNotification ~ date.getHours:',
date,
);
// Create a time-based trigger
const trigger = {
type: TriggerType.TIMESTAMP,
timestamp: date.getTime(), // fire at 11:10am (10 minutes before meeting)
};
// Create a trigger notification
await notifee.createTriggerNotification(
{
title: title,
body: message,
android: {
channelId: channelId,
},
},
trigger,
);
};
return {
requestUserPermission,
getFCMToken,
listenToForegroundNotifications,
listenToBackgroundNotifications,
onNotificationOpenedAppFromBackground,
onNotificationOpenedAppFromQuit,
scheduleNotification,
};
};
export default usePushNotification;