-
Notifications
You must be signed in to change notification settings - Fork 592
/
firebase.js
66 lines (56 loc) · 2.08 KB
/
firebase.js
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
import lang from 'i18n-js';
import { get } from 'lodash';
import firebase from 'react-native-firebase';
import { Alert } from '../components/alerts';
import { getLocal, saveLocal } from '../handlers/localstorage/common';
export const getFCMToken = async () => {
const fcmTokenLocal = await getLocal('rainbowFcmToken');
const fcmToken = get(fcmTokenLocal, 'data', null);
if (!fcmToken) {
throw new Error('Push notification token unavailable.');
}
return fcmToken;
};
export const saveFCMToken = async () => {
try {
const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
saveLocal('rainbowFcmToken', { data: fcmToken });
}
} catch (error) {
console.log('error getting fcm token');
}
};
export const hasPermission = async () => firebase.messaging().hasPermission();
export const requestPermission = async () => firebase.messaging().requestPermission();
export const checkPushNotificationPermissions = async () => {
const arePushNotificationsAuthorized = await hasPermission();
if (!arePushNotificationsAuthorized) {
Alert({
buttons: [
{
onPress: requestPermission,
text: 'Okay',
},
{
style: 'cancel',
text: 'Dismiss',
},
],
message: lang.t('wallet.push_notifications.please_enable_body'),
title: lang.t('wallet.push_notifications.please_enable_title'),
});
}
};
export const registerTokenRefreshListener = () => firebase.messaging().onTokenRefresh(fcmToken => {
saveLocal('rainbowFcmToken', { data: fcmToken });
});
export const registerNotificationListener = () => firebase.notifications().onNotification(notification => {
console.log('onNotification');
});
// TODO this.onPushNotificationOpened
export const registerNotificationOpenedListener = () => firebase.notifications().onNotificationOpened(notificationOpen => {
const { callId, sessionId } = notificationOpen.notification.data;
this.onPushNotificationOpened(callId, sessionId, false);
});
export const getInitialNotification = () => firebase.notifications().getInitialNotification();