@amrshbib/react-native-pushbib 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +475 -0
- package/android/build.gradle +51 -0
- package/android/src/main/AndroidManifest.xml +15 -0
- package/android/src/main/java/com/amrshbib/pushbib/FirebaseBootstrap.kt +37 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdatePayload.kt +102 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdateRenderer.kt +294 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationPayload.kt +130 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationRenderer.kt +181 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibChannels.kt +281 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibEmitter.kt +133 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibMessagingService.kt +87 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibModule.kt +286 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibPackage.kt +14 -0
- package/index.d.ts +224 -0
- package/ios/PushbibCenterDelegate.h +16 -0
- package/ios/PushbibCenterDelegate.m +138 -0
- package/ios/PushbibEventBus.h +34 -0
- package/ios/PushbibEventBus.m +88 -0
- package/ios/PushbibLiveActivities.h +18 -0
- package/ios/PushbibLiveActivities.m +46 -0
- package/ios/PushbibModule.h +5 -0
- package/ios/PushbibModule.m +260 -0
- package/ios/PushbibSwizzler.m +135 -0
- package/package.json +40 -0
- package/react-native-pushbib.podspec +19 -0
- package/src/EventManager.js +55 -0
- package/src/Logger.js +18 -0
- package/src/NativePushbib.js +36 -0
- package/src/api.js +108 -0
- package/src/index.js +753 -0
- package/src/version.js +2 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { log } from './Logger';
|
|
2
|
+
|
|
3
|
+
const VALID = [
|
|
4
|
+
'click', 'received', 'foregroundWillDisplay', 'permissionChange', 'pushSubscriptionChange',
|
|
5
|
+
'liveUpdate',
|
|
6
|
+
];
|
|
7
|
+
const BUFFERED = new Set(['click', 'received']);
|
|
8
|
+
const BUFFER_CAP = 20;
|
|
9
|
+
|
|
10
|
+
export class EventManager {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.listeners = new Map(VALID.map((n) => [n, new Set()]));
|
|
13
|
+
this.pending = new Map([...BUFFERED].map((n) => [n, []]));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
add(event, handler) {
|
|
17
|
+
const set = this.listeners.get(event);
|
|
18
|
+
if (!set) {
|
|
19
|
+
log.warn(`Unknown event "${event}" — valid events: ${VALID.join(', ')}`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
set.add(handler);
|
|
23
|
+
const queue = this.pending.get(event);
|
|
24
|
+
if (queue && queue.length > 0) {
|
|
25
|
+
const drained = queue.splice(0);
|
|
26
|
+
for (const ev of drained) this.emit(event, ev);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
remove(event, handler) {
|
|
31
|
+
this.listeners.get(event)?.delete(handler);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
count(event) {
|
|
35
|
+
return this.listeners.get(event)?.size || 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
emit(event, payload) {
|
|
39
|
+
const set = this.listeners.get(event);
|
|
40
|
+
if (BUFFERED.has(event) && (!set || set.size === 0)) {
|
|
41
|
+
const queue = this.pending.get(event);
|
|
42
|
+
if (queue.length >= BUFFER_CAP) queue.shift();
|
|
43
|
+
queue.push(payload);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
for (const handler of set || []) {
|
|
47
|
+
try {
|
|
48
|
+
handler(payload);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
log.error(`"${event}" listener threw:`, e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return (set?.size || 0) > 0;
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/Logger.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const LEVELS = { none: 0, error: 1, warn: 2, info: 3, verbose: 4 };
|
|
2
|
+
|
|
3
|
+
let current = LEVELS.warn;
|
|
4
|
+
|
|
5
|
+
export function setLogLevel(level) {
|
|
6
|
+
if (LEVELS[level] === undefined) {
|
|
7
|
+
console.warn(`[Pushbib] Unknown log level "${level}" — use ${Object.keys(LEVELS).join('/')}`);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
current = LEVELS[level];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const log = {
|
|
14
|
+
error: (...args) => current >= LEVELS.error && console.error('[Pushbib]', ...args),
|
|
15
|
+
warn: (...args) => current >= LEVELS.warn && console.warn('[Pushbib]', ...args),
|
|
16
|
+
info: (...args) => current >= LEVELS.info && console.log('[Pushbib]', ...args),
|
|
17
|
+
verbose: (...args) => current >= LEVELS.verbose && console.log('[Pushbib]', ...args),
|
|
18
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
|
|
2
|
+
|
|
3
|
+
const LINKING_ERROR =
|
|
4
|
+
`@amrshbib/react-native-pushbib: the native module is not linked.\n` +
|
|
5
|
+
Platform.select({
|
|
6
|
+
ios: '- Run "cd ios && pod install"\n',
|
|
7
|
+
android: '- Rebuild the app (autolinking picks the module up at build time)\n',
|
|
8
|
+
default: '- This platform is not supported (iOS and Android only)\n',
|
|
9
|
+
}) +
|
|
10
|
+
'- Restart Metro and rebuild the app after installing the package.';
|
|
11
|
+
|
|
12
|
+
const native = NativeModules.Pushbib;
|
|
13
|
+
|
|
14
|
+
export const PushbibNative =
|
|
15
|
+
native ||
|
|
16
|
+
new Proxy(
|
|
17
|
+
{},
|
|
18
|
+
{
|
|
19
|
+
get() {
|
|
20
|
+
throw new Error(LINKING_ERROR);
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export const isNativeAvailable = !!native;
|
|
26
|
+
|
|
27
|
+
export const nativeEmitter = native ? new NativeEventEmitter(native) : null;
|
|
28
|
+
|
|
29
|
+
export const NATIVE_EVENTS = {
|
|
30
|
+
pushToken: 'Pushbib#pushToken',
|
|
31
|
+
click: 'Pushbib#click',
|
|
32
|
+
received: 'Pushbib#received',
|
|
33
|
+
foregroundWillDisplay: 'Pushbib#foregroundWillDisplay',
|
|
34
|
+
liveUpdate: 'Pushbib#liveUpdate',
|
|
35
|
+
liveActivityToken: 'Pushbib#liveActivityToken',
|
|
36
|
+
};
|
package/src/api.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { log } from './Logger';
|
|
2
|
+
|
|
3
|
+
const TIMEOUT_MS = 15000;
|
|
4
|
+
|
|
5
|
+
export class ApiError extends Error {
|
|
6
|
+
constructor(status, message, body) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'PushbibApiError';
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.body = body;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class ApiClient {
|
|
15
|
+
configure({ baseUrl, appId, appKey, bundleId }) {
|
|
16
|
+
this.baseUrl = baseUrl.replace(/\/+$/, '') + '/api/mobile/v1';
|
|
17
|
+
this.headers = {
|
|
18
|
+
'Content-Type': 'application/json',
|
|
19
|
+
'X-Project-Id': appId,
|
|
20
|
+
Authorization: `Bearer ${appKey}`,
|
|
21
|
+
'X-Bundle-Id': bundleId,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async request(method, path, body) {
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
28
|
+
let res;
|
|
29
|
+
try {
|
|
30
|
+
res = await fetch(`${this.baseUrl}${path}`, {
|
|
31
|
+
method,
|
|
32
|
+
headers: this.headers,
|
|
33
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
34
|
+
signal: controller.signal,
|
|
35
|
+
});
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw new ApiError(0, `Network error calling ${path}: ${e.message}`, null);
|
|
38
|
+
} finally {
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let json = null;
|
|
43
|
+
try {
|
|
44
|
+
json = await res.json();
|
|
45
|
+
} catch (e) {
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!res.ok) {
|
|
49
|
+
throw new ApiError(res.status, json?.message || `HTTP ${res.status} on ${path}`, json);
|
|
50
|
+
}
|
|
51
|
+
log.verbose(`${method} ${path} → ${res.status}`);
|
|
52
|
+
return json;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get(path) {
|
|
56
|
+
return this.request('GET', path);
|
|
57
|
+
}
|
|
58
|
+
post(path, body) {
|
|
59
|
+
return this.request('POST', path, body);
|
|
60
|
+
}
|
|
61
|
+
delete(path, body) {
|
|
62
|
+
return this.request('DELETE', path, body);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getConfig() {
|
|
66
|
+
return this.get('/config');
|
|
67
|
+
}
|
|
68
|
+
registerDevice(payload) {
|
|
69
|
+
return this.post('/devices/register', payload);
|
|
70
|
+
}
|
|
71
|
+
unregisterDevice(payload) {
|
|
72
|
+
return this.post('/devices/unregister', payload);
|
|
73
|
+
}
|
|
74
|
+
pingSession(payload) {
|
|
75
|
+
return this.post('/devices/session', payload);
|
|
76
|
+
}
|
|
77
|
+
setLocation(payload) {
|
|
78
|
+
return this.post('/devices/location', payload);
|
|
79
|
+
}
|
|
80
|
+
clearLocation(payload) {
|
|
81
|
+
return this.delete('/devices/location', payload);
|
|
82
|
+
}
|
|
83
|
+
link(payload) {
|
|
84
|
+
return this.post('/auth/link', payload);
|
|
85
|
+
}
|
|
86
|
+
unlink(payload) {
|
|
87
|
+
return this.post('/auth/unlink', payload);
|
|
88
|
+
}
|
|
89
|
+
me(payload) {
|
|
90
|
+
return this.post('/me', payload);
|
|
91
|
+
}
|
|
92
|
+
addTags(payload) {
|
|
93
|
+
return this.post('/tags', payload);
|
|
94
|
+
}
|
|
95
|
+
removeTags(payload) {
|
|
96
|
+
return this.delete('/tags', payload);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
registerLiveActivity(payload) {
|
|
100
|
+
return this.post('/live-activity/register', payload);
|
|
101
|
+
}
|
|
102
|
+
endLiveActivity(payload) {
|
|
103
|
+
return this.post('/live-activity/end', payload);
|
|
104
|
+
}
|
|
105
|
+
liveActivitySessions(subscriptionId) {
|
|
106
|
+
return this.get(`/live-activity/sessions?subscription_id=${encodeURIComponent(subscriptionId)}`);
|
|
107
|
+
}
|
|
108
|
+
}
|