forked from oblador/react-native-ios-notification-actions
-
Notifications
You must be signed in to change notification settings - Fork 4
/
react-native-ios-notification-actions.ios.js
54 lines (43 loc) · 1.32 KB
/
react-native-ios-notification-actions.ios.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
import { NativeModules, NativeAppEventEmitter } from 'react-native';
const { RNNotificationActions } = NativeModules;
let actions = {};
function handleActionCompleted() {
RNNotificationActions.callCompletionHandler();
};
export class Action {
constructor(opts, onComplete) {
// TODO - check options
this.opts = opts;
this.onComplete = onComplete;
// When a notification is received, we'll call this action by it's identifier
actions[opts.identifier] = this;
NativeAppEventEmitter.addListener('notificationActionReceived', (body) => {
if (body.identifier === opts.identifier) {
onComplete(body, handleActionCompleted);
}
});
}
}
export class Category {
constructor(opts) {
// TODO - check options
this.opts = opts;
}
}
export const updateCategories = (categories, shouldRequestPermission = true) => {
let cats = categories.map((cat) => {
return Object.assign({}, cat.opts, {
actions: cat.opts.actions.map((action) => action.opts)
});
});
RNNotificationActions.updateCategories(cats, shouldRequestPermission);
// Re-update when permissions change
NativeAppEventEmitter.addListener('remoteNotificationsRegistered', () => {
RNNotificationActions.updateCategories(cats, false);
});
};
export default {
Action,
Category,
updateCategories
};