-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathcloudmessaging.js
108 lines (99 loc) · 4.17 KB
/
cloudmessaging.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
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
import {Platform, NativeModules, NativeEventEmitter} from 'react-native';
const FirestackCloudMessaging = NativeModules.FirestackCloudMessaging;
const FirestackCloudMessagingEvt = new NativeEventEmitter(FirestackCloudMessaging);
import promisify from '../utils/promisify'
import { Base, ReferenceBase } from './base'
const defaultPermissions = {
'badge': 1,
'sound': 2,
'alert': 3
}
export class CloudMessaging extends Base {
constructor(firestack, options = {}) {
super(firestack, options);
this.requestedPermissions = Object.assign({}, defaultPermissions, options.permissions);
}
get namespace() {
return 'firestack:cloudMessaging'
}
getToken() {
this.log.info('getToken for cloudMessaging');
return promisify('getToken', FirestackCloudMessaging)();
}
// Request FCM permissions
requestPermissions(requestedPermissions = {}) {
if (Platform.OS === 'ios') {
const mergedRequestedPermissions = Object.assign({},
this.requestedPermissions,
requestedPermissions);
return promisify('requestPermissions', FirestackCloudMessaging)(mergedRequestedPermissions)
.then(perms => {
return perms;
});
}
}
sendMessage(details:Object = {}, type:string='local') {
const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`
this.log.info('sendMessage', methodName, details);
return promisify(methodName, FirestackCloudMessaging)(details);
}
scheduleMessage(details:Object = {}, type:string='local') {
const methodName = `schedule${type == 'local' ? 'Local' : 'Remote'}`
return promisify(methodName, FirestackCloudMessaging)(details);
}
// OLD
send(senderId, messageId, messageType, msg){
return promisify('send', FirestackCloudMessaging)(senderId, messageId, messageType, msg);
}
//
listenForTokenRefresh(callback) {
this.log.info('Setting up listenForTokenRefresh callback');
const sub = this._on('FirestackRefreshToken', callback, FirestackCloudMessagingEvt);
return promisify(() => sub, FirestackCloudMessaging)(sub);
}
unlistenForTokenRefresh() {
this.log.info('Unlistening for TokenRefresh');
this._off('FirestackRefreshToken');
}
subscribeToTopic(topic) {
this.log.info('subscribeToTopic ' + topic);
const finalTopic = `/topics/${topic}`
return promisify('subscribeToTopic', FirestackCloudMessaging)(finalTopic);
}
unsubscribeFromTopic(topic) {
this.log.info('unsubscribeFromTopic ' + topic);
const finalTopic = `/topics/${topic}`
return promisify('unsubscribeFromTopic', FirestackCloudMessaging)(finalTopic);
}
// New api
onRemoteMessage(callback) {
this.log.info('On remote message callback');
const sub = this._on('messaging_remote_event_received', callback, FirestackCloudMessagingEvt);
return promisify(() => sub, FirestackCloudMessaging)(sub);
}
onLocalMessage(callback) {
this.log.info('on local callback');
const sub = this._on('messaging_local_event_received', callback, FirestackCloudMessagingEvt);
return promisify(() => sub, FirestackCloudMessaging)(sub);
}
// Original API
listenForReceiveNotification(callback) {
this.log.info('Setting up listenForReceiveNotification callback');
const sub = this._on('FirestackReceiveNotification', callback, FirestackCloudMessagingEvt);
return promisify(() => sub, FirestackCloudMessaging)(sub);
}
unlistenForReceiveNotification() {
this.log.info('Unlistening for ReceiveNotification');
this._off('FirestackRefreshToken');
}
listenForReceiveUpstreamSend(callback) {
this.log.info('Setting up send callback');
const sub = this._on('FirestackUpstreamSend', callback, FirestackCloudMessagingEvt);
return promisify(() => sub, FirestackCloudMessaging)(sub);
}
unlistenForReceiveUpstreamSend() {
this.log.info('Unlistening for send');
this._off('FirestackUpstreamSend');
}
}
export default CloudMessaging