-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.ts
147 lines (128 loc) · 4.42 KB
/
notifications.ts
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
import firebase from 'react-native-firebase';
import {Alert} from 'react-native';
export class NotificationService {
onTokenRefreshListener = null;
messageListener = null;
notificationDisplayedListener = null;
onNotificationListener = null;
onNotificationOpenedListener = null;
async getToken() {
const fcmToken = await firebase.messaging().getToken();
console.log(`Retrieved new token: ${fcmToken}`);
}
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
console.log('Messaging permissions enabled');
this.getToken();
} else {
this.requestPermission();
}
}
async requestPermission() {
console.log('Requesting messaging permissions');
try {
await firebase.messaging().requestPermission();
this.getToken();
} catch (error) {
console.log('Messaging permission rejected');
}
}
async createListeners() {
this.onTokenRefreshListener = firebase
.messaging()
.onTokenRefresh(async fcmToken => {
console.log(`Retrieved new token: ${fcmToken}`);
});
this.messageListener = firebase.messaging().onMessage(message => {
console.log('on Message');
console.log(message);
});
this.notificationDisplayedListener = firebase
.notifications()
.onNotificationDisplayed(notification => {
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
console.log('onNotificationDisplayed');
console.log(notification);
});
this.onNotificationListener = firebase
.notifications()
.onNotification(notification => {
console.log('onNotification');
console.log(notification);
// UNCOMMENT IF YOU WANT ANDROID TO DISPLAY THE NOTIFICATION
notification.android.setChannelId('default').setSound('default');
firebase.notifications().displayNotification(notification);
Alert.alert(
'Push Notification',
notification.body,
[{text: 'OK', onPress: () => console.log('OK Pressed')}],
{cancelable: false},
);
});
this.onNotificationOpenedListener = firebase
.notifications()
.onNotificationOpened(notification => {
console.log('onNotificationOpened');
console.log(notification);
Alert.alert(
'Push Notification',
`${notification.action},${notification.notification},${notification.results}`,
[{text: 'OK', onPress: () => console.log('OK Pressed')}],
{cancelable: false},
);
});
}
async scheduleNotification(date) {
const notification = new firebase.notifications.Notification()
.setNotificationId('1')
.setTitle('Test notification')
.setBody('This is a test notification')
.android.setPriority(firebase.notifications.Android.Priority.High)
.android.setChannelId('default')
.android.setAutoCancel(true);
firebase
.notifications()
.scheduleNotification(notification, {
fireDate: date.getTime(),
})
.catch(err => console.error(err));
}
configure() {
const channel = new firebase.notifications.Android.Channel(
'default',
'default channel',
firebase.notifications.Android.Importance.Max,
);
firebase.notifications().android.createChannel(channel);
this.checkPermission();
this.createListeners();
}
wasOpenedByNotification() {
firebase
.notifications()
.getInitialNotification()
.then(notificationOpen => {
if (notificationOpen) {
// App was opened by a notification
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification = notificationOpen.notification;
console.log('App was opened by a notification');
console.log(action);
console.log(notification);
Alert.alert(
'Push Notification',
`${action},${notification}`,
[{text: 'OK', onPress: () => console.log('OK Pressed')}],
{cancelable: false},
);
} else {
console.log('App was NOT opened by a notification');
}
});
}
}
const Notification = new NotificationService();
export default Notification;