-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseService.js
More file actions
71 lines (60 loc) · 1.67 KB
/
Copy pathfirebaseService.js
File metadata and controls
71 lines (60 loc) · 1.67 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
import { Platform } from 'react-native';
import {
getMessaging,
getToken,
onMessage,
onNotificationOpenedApp,
getInitialNotification,
requestPermission,
AuthorizationStatus,
} from '@react-native-firebase/messaging';
import { getApp } from '@react-native-firebase/app';
import Toast from 'react-native-toast-message';
/**
* Request permission to show notifications
*/
export async function requestUserPermission() {
const messaging = getMessaging(getApp());
const authStatus = await requestPermission(messaging);
const enabled =
authStatus === AuthorizationStatus.AUTHORIZED ||
authStatus === AuthorizationStatus.PROVISIONAL;
return enabled;
}
/**
* Get FCM token for this device
*/
export async function getFcmToken() {
const messaging = getMessaging(getApp());
try {
const token = await getToken(messaging);
console.log('FCM Token:', token);
return token;
} catch (error) {
console.log('Error getting FCM token:', error);
return null;
}
}
/**
* Listen to foreground messages
*/
export function onMessageListener() {
const messaging = getMessaging(getApp());
return onMessage(messaging, async remoteMessage => {
console.log('FCM Foreground message:', remoteMessage);
});
}
/**
* When app is opened from background by tapping notification
*/
export function onNotificationOpenedAppListener(callback) {
const messaging = getMessaging(getApp());
return onNotificationOpenedApp(messaging, callback);
}
/**
* When app is opened from quit state by tapping notification
*/
export async function getInitialNotificationListener() {
const messaging = getMessaging(getApp());
return getInitialNotification(messaging);
}