@amrshbib/react-native-pushbib 0.3.0 → 0.4.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 +30 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationRenderer.kt +17 -3
- package/android/src/main/java/com/amrshbib/pushbib/PushbibMessagingService.kt +5 -0
- package/android/src/main/java/com/amrshbib/pushbib/VoipPush.kt +32 -0
- package/android/src/main/res/layout/pushbib_notification_big_picture.xml +30 -0
- package/index.d.ts +12 -0
- package/ios/PushbibCenterDelegate.m +15 -0
- package/package.json +1 -1
- package/src/api.js +7 -0
- package/src/index.js +62 -0
- package/src/version.js +1 -1
package/README.md
CHANGED
|
@@ -429,6 +429,36 @@ const sessions = await Pushbib.LiveActivities.getSessions();
|
|
|
429
429
|
// [{ activity_id, status, activity_token_registered, last_sequence_number, … }]
|
|
430
430
|
```
|
|
431
431
|
|
|
432
|
+
## VoIP pushes — ring like a call
|
|
433
|
+
|
|
434
|
+
`POST /api/server/v1/voip/send` (server key) with `{ subscription_ids, data, ttl }` rings the
|
|
435
|
+
phone: a PushKit VoIP push on iOS, a high-priority data-only FCM message on Android. The SDK never
|
|
436
|
+
shows these — the call screen is the app's own (CallKit on iOS, a call-style notification on
|
|
437
|
+
Android). `data` values are strings; they reach the app as they were sent.
|
|
438
|
+
|
|
439
|
+
**iOS.** Add `voip` to `UIBackgroundModes`, register a `PKPushRegistry` in native code at launch,
|
|
440
|
+
and give the SDK its token:
|
|
441
|
+
|
|
442
|
+
```js
|
|
443
|
+
Pushbib.VoIP.setToken(hexToken); // re-sent by itself when the device gets a new subscription
|
|
444
|
+
Pushbib.VoIP.clearToken(); // stop ringing this device
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Every VoIP push must be reported to CallKit (`reportNewIncomingCall`) before the PushKit callback
|
|
448
|
+
returns — iOS stops delivering VoIP pushes to an app that doesn't. The payload is the `data` you
|
|
449
|
+
sent plus `pushbib_voip: "1"`.
|
|
450
|
+
|
|
451
|
+
**Android.** Nothing to register: the SDK hands the message to your app as a broadcast to its own
|
|
452
|
+
package, which a manifest receiver gets even when the app is not running:
|
|
453
|
+
|
|
454
|
+
```xml
|
|
455
|
+
<receiver android:name=".IncomingCallReceiver" android:exported="false">
|
|
456
|
+
<intent-filter><action android:name="com.amrshbib.pushbib.VOIP_PUSH" /></intent-filter>
|
|
457
|
+
</receiver>
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
Each `data` key arrives as a string extra on the intent.
|
|
461
|
+
|
|
432
462
|
## Android customization (optional)
|
|
433
463
|
|
|
434
464
|
`AndroidManifest.xml` meta-data, all optional:
|
|
@@ -11,6 +11,8 @@ import android.graphics.BitmapFactory
|
|
|
11
11
|
import android.graphics.Color
|
|
12
12
|
import android.media.RingtoneManager
|
|
13
13
|
import android.os.Build
|
|
14
|
+
import android.view.View
|
|
15
|
+
import android.widget.RemoteViews
|
|
14
16
|
import androidx.core.app.NotificationCompat
|
|
15
17
|
import androidx.core.app.NotificationManagerCompat
|
|
16
18
|
import org.json.JSONObject
|
|
@@ -73,9 +75,13 @@ object NotificationRenderer {
|
|
|
73
75
|
|
|
74
76
|
fetchBitmap(p.largeIcon)?.let { builder.setLargeIcon(it) }
|
|
75
77
|
fetchBitmap(p.imageUrl)?.let { image ->
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
// BigPictureStyle cuts the body to two lines, so the expanded view is our own:
|
|
79
|
+
// the whole body above the picture. Android 12+ wraps it in the app header by
|
|
80
|
+
// itself, full width, but drops the buttons there; a push with buttons, and older
|
|
81
|
+
// versions, get DecoratedCustomViewStyle instead.
|
|
82
|
+
val bare = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && p.buttons.isEmpty()
|
|
83
|
+
builder.setStyle(if (bare) null else NotificationCompat.DecoratedCustomViewStyle())
|
|
84
|
+
builder.setCustomBigContentView(bigPictureView(context, p, image))
|
|
79
85
|
if (p.largeIcon == null) builder.setLargeIcon(image)
|
|
80
86
|
}
|
|
81
87
|
|
|
@@ -91,6 +97,14 @@ object NotificationRenderer {
|
|
|
91
97
|
}
|
|
92
98
|
}
|
|
93
99
|
|
|
100
|
+
private fun bigPictureView(context: Context, p: NotificationPayload, image: Bitmap) =
|
|
101
|
+
RemoteViews(context.packageName, R.layout.pushbib_notification_big_picture).apply {
|
|
102
|
+
setTextViewText(R.id.pushbib_title, p.title ?: appName(context))
|
|
103
|
+
setTextViewText(R.id.pushbib_body, p.body)
|
|
104
|
+
setViewVisibility(R.id.pushbib_body, if (p.body == null) View.GONE else View.VISIBLE)
|
|
105
|
+
setImageViewBitmap(R.id.pushbib_image, image)
|
|
106
|
+
}
|
|
107
|
+
|
|
94
108
|
private fun clickIntent(context: Context, p: NotificationPayload, actionId: String?, requestCode: Int): PendingIntent {
|
|
95
109
|
val launch = context.packageManager.getLaunchIntentForPackage(context.packageName)
|
|
96
110
|
?: Intent().setPackage(context.packageName)
|
|
@@ -9,6 +9,11 @@ import org.json.JSONObject
|
|
|
9
9
|
class PushbibMessagingService : FirebaseMessagingService() {
|
|
10
10
|
|
|
11
11
|
override fun onMessageReceived(message: RemoteMessage) {
|
|
12
|
+
if (VoipPush.isVoip(message)) {
|
|
13
|
+
VoipPush.forward(applicationContext, message)
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
if (LiveUpdatePayload.isLiveUpdate(message)) {
|
|
13
18
|
handleLiveUpdate(message)
|
|
14
19
|
return
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
package com.amrshbib.pushbib
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.Intent
|
|
5
|
+
import com.google.firebase.messaging.RemoteMessage
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A VoIP push (`POST /api/server/v1/voip/send`): a high-priority, data-only FCM message marked
|
|
9
|
+
* `pushbib_voip = "1"`. The SDK never renders it — ringing is the app's own job — so it is handed
|
|
10
|
+
* to the app as a broadcast to its own package, which a manifest receiver gets even when the app
|
|
11
|
+
* is not running:
|
|
12
|
+
*
|
|
13
|
+
* <receiver android:name=".IncomingCallReceiver" android:exported="false">
|
|
14
|
+
* <intent-filter><action android:name="com.amrshbib.pushbib.VOIP_PUSH" /></intent-filter>
|
|
15
|
+
* </receiver>
|
|
16
|
+
*
|
|
17
|
+
* Every data key but the marker comes as a string extra.
|
|
18
|
+
*/
|
|
19
|
+
object VoipPush {
|
|
20
|
+
const val ACTION = "com.amrshbib.pushbib.VOIP_PUSH"
|
|
21
|
+
private const val MARKER = "pushbib_voip"
|
|
22
|
+
|
|
23
|
+
fun isVoip(message: RemoteMessage): Boolean = message.data[MARKER] == "1"
|
|
24
|
+
|
|
25
|
+
fun forward(context: Context, message: RemoteMessage) {
|
|
26
|
+
val intent = Intent(ACTION).setPackage(context.packageName)
|
|
27
|
+
for ((key, value) in message.data) {
|
|
28
|
+
if (key != MARKER) intent.putExtra(key, value)
|
|
29
|
+
}
|
|
30
|
+
context.sendBroadcast(intent)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- The expanded view of a push with a picture: the whole body, then the picture
|
|
3
|
+
scaled into the height that is left, so neither is cut. -->
|
|
4
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
5
|
+
android:layout_width="match_parent"
|
|
6
|
+
android:layout_height="wrap_content"
|
|
7
|
+
android:orientation="vertical">
|
|
8
|
+
|
|
9
|
+
<TextView
|
|
10
|
+
android:id="@+id/pushbib_title"
|
|
11
|
+
style="@style/TextAppearance.Compat.Notification.Title"
|
|
12
|
+
android:layout_width="match_parent"
|
|
13
|
+
android:layout_height="wrap_content"
|
|
14
|
+
android:ellipsize="end"
|
|
15
|
+
android:maxLines="2" />
|
|
16
|
+
|
|
17
|
+
<TextView
|
|
18
|
+
android:id="@+id/pushbib_body"
|
|
19
|
+
style="@style/TextAppearance.Compat.Notification"
|
|
20
|
+
android:layout_width="match_parent"
|
|
21
|
+
android:layout_height="wrap_content" />
|
|
22
|
+
|
|
23
|
+
<ImageView
|
|
24
|
+
android:id="@+id/pushbib_image"
|
|
25
|
+
android:layout_width="match_parent"
|
|
26
|
+
android:layout_height="wrap_content"
|
|
27
|
+
android:layout_marginTop="8dp"
|
|
28
|
+
android:adjustViewBounds="true"
|
|
29
|
+
android:scaleType="fitCenter" />
|
|
30
|
+
</LinearLayout>
|
package/index.d.ts
CHANGED
|
@@ -293,6 +293,17 @@ export interface ChannelsNamespace {
|
|
|
293
293
|
openSettings(key?: string): Promise<boolean>;
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
+
/**
|
|
297
|
+
* VoIP pushes (the server's `POST /api/server/v1/voip/send`) ring the phone like a call; the SDK
|
|
298
|
+
* never shows them. iOS: hand the PushKit token over with setToken (and report every VoIP push to
|
|
299
|
+
* CallKit at once). Android: the FCM token rings — the message comes to the app's
|
|
300
|
+
* `com.amrshbib.pushbib.VOIP_PUSH` broadcast receiver. Both never reject (null on failure).
|
|
301
|
+
*/
|
|
302
|
+
export interface VoipNamespace {
|
|
303
|
+
setToken(token: string): Promise<{ subscription_id: string; voip_registered: boolean } | null>;
|
|
304
|
+
clearToken(): Promise<unknown>;
|
|
305
|
+
}
|
|
306
|
+
|
|
296
307
|
export interface PushbibStatic {
|
|
297
308
|
initialize(options: InitializeOptions): void;
|
|
298
309
|
/**
|
|
@@ -321,6 +332,7 @@ export interface PushbibStatic {
|
|
|
321
332
|
Location: LocationNamespace;
|
|
322
333
|
LiveActivities: LiveActivitiesNamespace;
|
|
323
334
|
Channels: ChannelsNamespace;
|
|
335
|
+
VoIP: VoipNamespace;
|
|
324
336
|
}
|
|
325
337
|
|
|
326
338
|
declare const Pushbib: PushbibStatic;
|
|
@@ -124,6 +124,21 @@ static BOOL PushbibFlag(NSDictionary *userInfo, NSString *key, BOOL fallback) {
|
|
|
124
124
|
if ([parsed isKindOfClass:NSDictionary.class]) data = parsed;
|
|
125
125
|
} else if ([blob isKindOfClass:NSDictionary.class]) {
|
|
126
126
|
data = blob;
|
|
127
|
+
} else if (blob == nil) {
|
|
128
|
+
/* A legacy (raw APNs token) send writes `data` straight into the payload, next to `aps`
|
|
129
|
+
* (services/push/apns.service.js on the server), so there is no pushbib_data to read.
|
|
130
|
+
* Everything at the top level that is not APNs' own or a field read below is the data. */
|
|
131
|
+
NSMutableDictionary *loose = [NSMutableDictionary new];
|
|
132
|
+
for (id key in userInfo) {
|
|
133
|
+
if (![key isKindOfClass:NSString.class]) continue;
|
|
134
|
+
NSString *name = (NSString *)key;
|
|
135
|
+
if ([name isEqualToString:@"aps"] || [name isEqualToString:@"url"] || [name isEqualToString:@"imageUrl"] ||
|
|
136
|
+
[name hasPrefix:@"gcm."] || [name hasPrefix:@"google."] || [name hasPrefix:@"pushbib_"]) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
loose[name] = userInfo[key];
|
|
140
|
+
}
|
|
141
|
+
data = loose;
|
|
127
142
|
}
|
|
128
143
|
|
|
129
144
|
NSMutableDictionary *notification = [NSMutableDictionary new];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amrshbib/react-native-pushbib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Self-hosted push notification SDK for React Native. Pushbib-style API (login, tags, click events) with zero Firebase setup in the app \u2014 no google-services.json, no GoogleService-Info.plist.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
package/src/api.js
CHANGED
|
@@ -111,4 +111,11 @@ export class ApiClient {
|
|
|
111
111
|
liveActivitySessions(subscriptionId) {
|
|
112
112
|
return this.get(`/live-activity/sessions?subscription_id=${encodeURIComponent(subscriptionId)}`);
|
|
113
113
|
}
|
|
114
|
+
|
|
115
|
+
registerVoip(payload) {
|
|
116
|
+
return this.post('/voip/register', payload);
|
|
117
|
+
}
|
|
118
|
+
removeVoip(payload) {
|
|
119
|
+
return this.delete('/voip', payload);
|
|
120
|
+
}
|
|
114
121
|
}
|
package/src/index.js
CHANGED
|
@@ -65,6 +65,9 @@ function buildNotification(raw, display) {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
// A PushKit token: 32 bytes today, as hex
|
|
69
|
+
const VOIP_TOKEN = /^[0-9a-f]{64,200}$/i;
|
|
70
|
+
|
|
68
71
|
const PREFS = {
|
|
69
72
|
subscriptionId: 'subscription_id',
|
|
70
73
|
externalId: 'external_id',
|
|
@@ -151,6 +154,8 @@ class PushbibSdk {
|
|
|
151
154
|
language: null,
|
|
152
155
|
locationShared: true,
|
|
153
156
|
permissionAsked: false,
|
|
157
|
+
// iOS: the PushKit token VoIP pushes ring through (VoIP.setToken); never stored, the app gives it each launch
|
|
158
|
+
voipToken: null,
|
|
154
159
|
};
|
|
155
160
|
this._needsRegister = false;
|
|
156
161
|
this._lastSessionPingAt = 0;
|
|
@@ -172,6 +177,7 @@ class PushbibSdk {
|
|
|
172
177
|
this.Location = this._buildLocationNamespace();
|
|
173
178
|
this.LiveActivities = this._buildLiveActivitiesNamespace();
|
|
174
179
|
this.Channels = this._buildChannelsNamespace();
|
|
180
|
+
this.VoIP = this._buildVoipNamespace();
|
|
175
181
|
}
|
|
176
182
|
|
|
177
183
|
initialize(options) {
|
|
@@ -637,6 +643,59 @@ class PushbibSdk {
|
|
|
637
643
|
return ns;
|
|
638
644
|
}
|
|
639
645
|
|
|
646
|
+
/**
|
|
647
|
+
* VoIP pushes (`POST /api/server/v1/voip/send` on the server) ring the phone like a call; the SDK
|
|
648
|
+
* never shows them. iOS rings through its own PushKit token, which the app's PushKit delegate
|
|
649
|
+
* hands over with setToken (and the app must report every VoIP push to CallKit right away).
|
|
650
|
+
* Android rings through the normal FCM token: the SDK passes the message to the app's
|
|
651
|
+
* `com.amrshbib.pushbib.VOIP_PUSH` receiver (VoipPush.kt), so nothing is needed here.
|
|
652
|
+
*
|
|
653
|
+
* Both never reject: a failure is logged and resolves to null. The token is sent again whenever
|
|
654
|
+
* the device gets a new subscription.
|
|
655
|
+
*/
|
|
656
|
+
_buildVoipNamespace() {
|
|
657
|
+
const sdk = this;
|
|
658
|
+
const quiet = (label, promise) =>
|
|
659
|
+
promise.catch((e) => {
|
|
660
|
+
log.warn(`VoIP.${label} failed:`, e.message || e);
|
|
661
|
+
return null;
|
|
662
|
+
});
|
|
663
|
+
return {
|
|
664
|
+
setToken(token) {
|
|
665
|
+
const value = typeof token === 'string' ? token.trim() : '';
|
|
666
|
+
if (!VOIP_TOKEN.test(value)) {
|
|
667
|
+
log.warn('VoIP.setToken needs the PushKit token as a hex string — ignored');
|
|
668
|
+
return Promise.resolve(null);
|
|
669
|
+
}
|
|
670
|
+
sdk._state.voipToken = value;
|
|
671
|
+
return quiet('setToken', sdk._sendVoipToken());
|
|
672
|
+
},
|
|
673
|
+
clearToken() {
|
|
674
|
+
sdk._state.voipToken = null;
|
|
675
|
+
return quiet('clearToken', sdk._sendVoipToken());
|
|
676
|
+
},
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// The VoIP token as it is now (none = remove it), for the current subscription.
|
|
681
|
+
_sendVoipToken() {
|
|
682
|
+
return this._enqueue(async () => {
|
|
683
|
+
const subscriptionId = this._state.subscriptionId;
|
|
684
|
+
if (!subscriptionId) return null;
|
|
685
|
+
const token = this._state.voipToken;
|
|
686
|
+
if (token) {
|
|
687
|
+
const res = await this._api.registerVoip({ subscription_id: subscriptionId, token });
|
|
688
|
+
log.info('VoIP token registered');
|
|
689
|
+
return res.data;
|
|
690
|
+
}
|
|
691
|
+
const res = await this._api.removeVoip({ subscription_id: subscriptionId }).catch((e) => {
|
|
692
|
+
if (e instanceof ApiError && e.status === 404) return { data: null };
|
|
693
|
+
throw e;
|
|
694
|
+
});
|
|
695
|
+
return res.data;
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
640
699
|
_buildNotificationsNamespace() {
|
|
641
700
|
const sdk = this;
|
|
642
701
|
return {
|
|
@@ -1059,6 +1118,9 @@ class PushbibSdk {
|
|
|
1059
1118
|
this._state.registered = !this._state.optedOut;
|
|
1060
1119
|
if (res.data.subscription_id !== previousId) {
|
|
1061
1120
|
await this._prefsSet(PREFS.subscriptionId, res.data.subscription_id);
|
|
1121
|
+
if (this._state.voipToken) {
|
|
1122
|
+
this._sendVoipToken().catch((e) => log.warn('VoIP token not sent again:', e.message || e));
|
|
1123
|
+
}
|
|
1062
1124
|
}
|
|
1063
1125
|
log.info(`registered (subscription ${res.data.subscription_id})`);
|
|
1064
1126
|
this._events.emit('pushSubscriptionChange', await this._subscriptionSnapshot());
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const SDK_VERSION = '0.
|
|
1
|
+
export const SDK_VERSION = '0.4.1';
|
|
2
2
|
export const SDK_NAME = `pushbib-rn/${SDK_VERSION}`;
|