Skip to content

Commit 7436d00

Browse files
committed
initial commit
1 parent 20de71f commit 7436d00

File tree

6 files changed

+706
-0
lines changed

6 files changed

+706
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# CUSTOM
2+
#
3+
.*.swp
4+
5+
# OSX
6+
#
7+
.DS_Store
8+
9+
# Xcode
10+
#
11+
build/
12+
*.pbxuser
13+
!default.pbxuser
14+
*.mode1v3
15+
!default.mode1v3
16+
*.mode2v3
17+
!default.mode2v3
18+
*.perspectivev3
19+
!default.perspectivev3
20+
xcuserdata
21+
*.xccheckout
22+
*.moved-aside
23+
DerivedData
24+
*.hmap
25+
*.ipa
26+
*.xcuserstate
27+
project.xcworkspace
28+
29+
# Android/IJ
30+
#
31+
.idea
32+
.gradle
33+
local.properties
34+
35+
# node.js
36+
#
37+
node_modules/
38+
npm-debug.log

index.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
'use strict';
2+
3+
import {
4+
NativeModules,
5+
DeviceEventEmitter,
6+
} from 'react-native';
7+
8+
var RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager;
9+
var invariant = require('fbjs/lib/invariant');
10+
11+
var _notifHandlers = new Map();
12+
13+
var DEVICE_NOTIF_EVENT = 'voipRemoteNotificationReceived';
14+
var NOTIF_REGISTER_EVENT = 'voipRemoteNotificationsRegistered';
15+
var DEVICE_LOCAL_NOTIF_EVENT = 'voipLocalNotificationReceived';
16+
17+
export default class RNVoipPushNotification {
18+
19+
/**
20+
* Schedules the localNotification for immediate presentation.
21+
*
22+
* details is an object containing:
23+
*
24+
* - `alertBody` : The message displayed in the notification alert.
25+
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
26+
* - `soundName` : The sound played when the notification is fired (optional).
27+
* - `category` : The category of this notification, required for actionable notifications (optional).
28+
* - `userInfo` : An optional object containing additional notification data.
29+
*/
30+
static presentLocalNotification(details) {
31+
RNVoipPushNotificationManager.presentLocalNotification(details);
32+
}
33+
34+
/**
35+
* Attaches a listener to remote notification events while the app is running
36+
* in the foreground or the background.
37+
*
38+
* Valid events are:
39+
*
40+
* - `notification` : Fired when a remote notification is received. The
41+
* handler will be invoked with an instance of `PushNotificationIOS`.
42+
* - `register`: Fired when the user registers for remote notifications. The
43+
* handler will be invoked with a hex string representing the deviceToken.
44+
*/
45+
static addEventListener(type, handler) {
46+
invariant(
47+
type === 'notification' || type === 'register' || type === 'localNotification',
48+
'RNVoipPushNotificationManager only supports `notification`, `register` and `localNotification` events'
49+
);
50+
var listener;
51+
if (type === 'notification') {
52+
listener = DeviceEventEmitter.addListener(
53+
DEVICE_NOTIF_EVENT,
54+
(notifData) => {
55+
handler(new RNVoipPushNotification(notifData));
56+
}
57+
);
58+
} else if (type === 'localNotification') {
59+
listener = DeviceEventEmitter.addListener(
60+
DEVICE_LOCAL_NOTIF_EVENT,
61+
(notifData) => {
62+
handler(new RNVoipPushNotification(notifData));
63+
}
64+
);
65+
} else if (type === 'register') {
66+
listener = DeviceEventEmitter.addListener(
67+
NOTIF_REGISTER_EVENT,
68+
(registrationInfo) => {
69+
handler(registrationInfo.deviceToken);
70+
}
71+
);
72+
}
73+
_notifHandlers.set(handler, listener);
74+
}
75+
76+
/**
77+
* Removes the event listener. Do this in `componentWillUnmount` to prevent
78+
* memory leaks
79+
*/
80+
static removeEventListener(type, handler) {
81+
invariant(
82+
type === 'notification' || type === 'register' || type === 'localNotification',
83+
'RNVoipPushNotification only supports `notification`, `register` and `localNotification` events'
84+
);
85+
var listener = _notifHandlers.get(handler);
86+
if (!listener) {
87+
return;
88+
}
89+
listener.remove();
90+
_notifHandlers.delete(handler);
91+
}
92+
93+
/**
94+
* Requests notification permissions from iOS, prompting the user's
95+
* dialog box. By default, it will request all notification permissions, but
96+
* a subset of these can be requested by passing a map of requested
97+
* permissions.
98+
* The following permissions are supported:
99+
*
100+
* - `alert`
101+
* - `badge`
102+
* - `sound`
103+
*
104+
* If a map is provided to the method, only the permissions with truthy values
105+
* will be requested.
106+
*/
107+
static requestPermissions(permissions) {
108+
var requestedPermissions = {};
109+
if (permissions) {
110+
requestedPermissions = {
111+
alert: !!permissions.alert,
112+
badge: !!permissions.badge,
113+
sound: !!permissions.sound
114+
};
115+
} else {
116+
requestedPermissions = {
117+
alert: true,
118+
badge: true,
119+
sound: true
120+
};
121+
}
122+
RNVoipPushNotificationManager.requestPermissions(requestedPermissions);
123+
}
124+
125+
/**
126+
* You will never need to instantiate `RNVoipPushNotification` yourself.
127+
* Listening to the `notification` event and invoking
128+
* `popInitialNotification` is sufficient
129+
*/
130+
constructor(nativeNotif) {
131+
this._data = {};
132+
133+
// Extract data from Apple's `aps` dict as defined:
134+
135+
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
136+
137+
Object.keys(nativeNotif).forEach((notifKey) => {
138+
var notifVal = nativeNotif[notifKey];
139+
if (notifKey === 'aps') {
140+
this._alert = notifVal.alert;
141+
this._sound = notifVal.sound;
142+
this._badgeCount = notifVal.badge;
143+
} else {
144+
this._data[notifKey] = notifVal;
145+
}
146+
});
147+
}
148+
149+
/**
150+
* An alias for `getAlert` to get the notification's main message string
151+
*/
152+
getMessage() {
153+
// alias because "alert" is an ambiguous name
154+
return this._alert;
155+
}
156+
157+
/**
158+
* Gets the sound string from the `aps` object
159+
*/
160+
getSound() {
161+
return this._sound;
162+
}
163+
164+
/**
165+
* Gets the notification's main message from the `aps` object
166+
*/
167+
getAlert() {
168+
return this._alert;
169+
}
170+
171+
/**
172+
* Gets the badge count number from the `aps` object
173+
*/
174+
getBadgeCount() {
175+
return this._badgeCount;
176+
}
177+
178+
/**
179+
* Gets the data object on the notif
180+
*/
181+
getData() {
182+
return this._data;
183+
}
184+
}

0 commit comments

Comments
 (0)