@amrshbib/react-native-pushbib 0.2.0 → 0.3.0
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 +28 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationPayload.kt +9 -12
- package/index.d.ts +11 -2
- package/ios/PushbibCenterDelegate.m +12 -11
- package/package.json +1 -1
- package/src/index.js +8 -25
- package/src/version.js +1 -1
package/README.md
CHANGED
|
@@ -534,6 +534,34 @@ on the dashboard's device list as **unsubscribed** — with its token, tags, lan
|
|
|
534
534
|
count intact — so it reappears the moment `optIn()` is called. Only an explicit delete in the
|
|
535
535
|
dashboard removes a device.
|
|
536
536
|
|
|
537
|
+
A received or clicked notification carries OneSignal's field names as well as Pushbib's own,
|
|
538
|
+
so one handler reads either SDK's event with no branch per provider:
|
|
539
|
+
|
|
540
|
+
| OneSignal | Pushbib's own name |
|
|
541
|
+
| --- | --- |
|
|
542
|
+
| `additionalData` | `data` |
|
|
543
|
+
| `launchURL` | `url` |
|
|
544
|
+
| `actionButtons` | `buttons` |
|
|
545
|
+
| `bigPicture` | `imageUrl` |
|
|
546
|
+
| `groupKey` | `collapseId` |
|
|
547
|
+
| `rawPayload` | the native payload as delivered |
|
|
548
|
+
|
|
549
|
+
`additionalData` arrives with the shape it was sent, exactly as OneSignal's does — a nested
|
|
550
|
+
object, a number or a boolean comes back as one, and a string the sender wrote stays a string
|
|
551
|
+
even when it holds JSON text:
|
|
552
|
+
|
|
553
|
+
```js
|
|
554
|
+
// sent as { type: 'Messaging', queries: { featureId: 205 }, orderData: '{"Status":{"Id":3}}' }
|
|
555
|
+
Pushbib.Notifications.addEventListener('click', (e) => {
|
|
556
|
+
const { featureId } = e.notification.additionalData.queries; // 205, a number
|
|
557
|
+
JSON.parse(e.notification.additionalData.orderData); // still the string the sender wrote
|
|
558
|
+
});
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
FCM's data map carries strings and nothing else, so the server sends the whole thing as one
|
|
562
|
+
JSON string and the SDK reads it back — the same trick OneSignal plays with `custom`. Nothing
|
|
563
|
+
about it is visible from JS.
|
|
564
|
+
|
|
537
565
|
Two deliberate differences:
|
|
538
566
|
|
|
539
567
|
- **Nothing throws on empty or odd input.** `addTags({})`, `removeTags([])`, `login('')`,
|
|
@@ -18,7 +18,7 @@ class NotificationPayload private constructor(
|
|
|
18
18
|
val collapseId: String?,
|
|
19
19
|
val url: String?,
|
|
20
20
|
val buttons: List<Button>,
|
|
21
|
-
val data:
|
|
21
|
+
val data: JSONObject,
|
|
22
22
|
val notificationId: Int,
|
|
23
23
|
val showForeground: Boolean,
|
|
24
24
|
val showBackground: Boolean,
|
|
@@ -42,13 +42,10 @@ class NotificationPayload private constructor(
|
|
|
42
42
|
putOpt("collapseId", collapseId)
|
|
43
43
|
putOpt("url", url)
|
|
44
44
|
put("buttons", JSONArray().apply { buttons.forEach { put(JSONObject().put("id", it.id).put("text", it.text)) } })
|
|
45
|
-
put("data",
|
|
45
|
+
put("data", data)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
companion object {
|
|
49
|
-
private val CONTROL_KEYS =
|
|
50
|
-
setOf("pushbib_payload", "url", "buttons", "largeIcon", "showForeground", "showBackground")
|
|
51
|
-
|
|
52
49
|
private fun flag(raw: String?, default: Boolean = true): Boolean =
|
|
53
50
|
when (raw?.trim()?.lowercase()) {
|
|
54
51
|
null, "" -> default
|
|
@@ -62,8 +59,12 @@ class NotificationPayload private constructor(
|
|
|
62
59
|
val notif = message.notification
|
|
63
60
|
if (display == null && notif == null) return null
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
/* The data map FCM delivers holds strings and nothing else, so what the sender
|
|
63
|
+
* wrote — a nested object, a number, a flag — travels whole as JSON under one
|
|
64
|
+
* key. Reading that back is what hands the app the shape it was sent. */
|
|
65
|
+
val custom = data["pushbib_data"]
|
|
66
|
+
?.let { runCatching { JSONObject(it) }.getOrNull() }
|
|
67
|
+
?: JSONObject()
|
|
67
68
|
|
|
68
69
|
return NotificationPayload(
|
|
69
70
|
title = display?.optNonEmpty("title") ?: notif?.title,
|
|
@@ -100,11 +101,7 @@ class NotificationPayload private constructor(
|
|
|
100
101
|
collapseId = json.optNonEmpty("collapseId"),
|
|
101
102
|
url = json.optNonEmpty("url"),
|
|
102
103
|
buttons = parseButtons(json.optJSONArray("buttons")),
|
|
103
|
-
data = json.optJSONObject("data")
|
|
104
|
-
val out = HashMap<String, String>()
|
|
105
|
-
obj.keys().forEach { k -> out[k] = obj.optString(k) }
|
|
106
|
-
out
|
|
107
|
-
} ?: emptyMap(),
|
|
104
|
+
data = json.optJSONObject("data") ?: JSONObject(),
|
|
108
105
|
notificationId = json.optInt("notificationId", (System.currentTimeMillis() and 0x7FFFFFFF).toInt()),
|
|
109
106
|
showForeground = json.optBoolean("showForeground", true),
|
|
110
107
|
showBackground = json.optBoolean("showBackground", true),
|
package/index.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export interface NotificationButton {
|
|
|
37
37
|
|
|
38
38
|
export interface PushbibNotification {
|
|
39
39
|
/** OneSignal's name for `data`. */
|
|
40
|
-
additionalData: Record<string,
|
|
40
|
+
additionalData: Record<string, unknown>;
|
|
41
41
|
/** OneSignal's name for `url`. */
|
|
42
42
|
launchURL?: string | null;
|
|
43
43
|
/** Puts the notification back on screen after preventDefault(); a no-op otherwise. */
|
|
@@ -51,7 +51,16 @@ export interface PushbibNotification {
|
|
|
51
51
|
largeIcon?: string;
|
|
52
52
|
url?: string;
|
|
53
53
|
buttons?: NotificationButton[];
|
|
54
|
-
|
|
54
|
+
/** What the sender wrote, with its shape: a nested object, a number or a flag arrives as one. */
|
|
55
|
+
data: Record<string, unknown>;
|
|
56
|
+
/** OneSignal's name for `buttons`. */
|
|
57
|
+
actionButtons: NotificationButton[];
|
|
58
|
+
/** OneSignal's name for `imageUrl`. */
|
|
59
|
+
bigPicture: string | null;
|
|
60
|
+
/** OneSignal's name for `collapseId`. */
|
|
61
|
+
groupKey: string | null;
|
|
62
|
+
/** Everything the native side delivered, before the OneSignal names were added. */
|
|
63
|
+
rawPayload: Record<string, unknown>;
|
|
55
64
|
}
|
|
56
65
|
|
|
57
66
|
export interface NotificationClickEvent {
|
|
@@ -112,17 +112,18 @@ static BOOL PushbibFlag(NSDictionary *userInfo, NSString *key, BOOL fallback) {
|
|
|
112
112
|
|
|
113
113
|
+ (NSDictionary *)normalizeContent:(UNNotificationContent *)content {
|
|
114
114
|
NSDictionary *userInfo = content.userInfo ?: @{};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if ([
|
|
125
|
-
|
|
115
|
+
/* The same message is built for a transport whose data map holds strings and nothing
|
|
116
|
+
* else, so what the sender wrote travels whole as JSON under one key. Reading that
|
|
117
|
+
* back is what hands the app the shape it was sent — a nested object, a number, a
|
|
118
|
+
* flag. A push written straight to APNs carries it as real JSON already. */
|
|
119
|
+
NSDictionary *data = @{};
|
|
120
|
+
id blob = userInfo[@"pushbib_data"];
|
|
121
|
+
if ([blob isKindOfClass:NSString.class]) {
|
|
122
|
+
NSData *raw = [(NSString *)blob dataUsingEncoding:NSUTF8StringEncoding];
|
|
123
|
+
id parsed = raw ? [NSJSONSerialization JSONObjectWithData:raw options:0 error:NULL] : nil;
|
|
124
|
+
if ([parsed isKindOfClass:NSDictionary.class]) data = parsed;
|
|
125
|
+
} else if ([blob isKindOfClass:NSDictionary.class]) {
|
|
126
|
+
data = blob;
|
|
126
127
|
}
|
|
127
128
|
|
|
128
129
|
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.3.0",
|
|
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/index.js
CHANGED
|
@@ -41,35 +41,12 @@ function tagItems(value) {
|
|
|
41
41
|
return [String(value)];
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
// Android carries a notification's data as strings and nothing else, so a nested value —
|
|
45
|
-
// queries: { featureId } — arrives as the JSON text of itself, while iOS carries the real thing.
|
|
46
|
-
// Reading it back here is what lets an app written once see the same shape on both.
|
|
47
|
-
function reviveData(value) {
|
|
48
|
-
if (Array.isArray(value)) return value.map(reviveData);
|
|
49
|
-
if (value && typeof value === 'object') {
|
|
50
|
-
const out = {};
|
|
51
|
-
for (const key of Object.keys(value)) out[key] = reviveData(value[key]);
|
|
52
|
-
return out;
|
|
53
|
-
}
|
|
54
|
-
if (typeof value !== 'string') return value;
|
|
55
|
-
|
|
56
|
-
const text = value.trim();
|
|
57
|
-
const shaped = (text.startsWith('{') && text.endsWith('}'))
|
|
58
|
-
|| (text.startsWith('[') && text.endsWith(']'));
|
|
59
|
-
if (!shaped) return value;
|
|
60
|
-
try {
|
|
61
|
-
return reviveData(JSON.parse(text));
|
|
62
|
-
} catch (err) {
|
|
63
|
-
// It only looked like JSON. What the sender wrote is what the app gets.
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
44
|
// Notification events carry OneSignal's shape as well as Pushbib's own fields: callers can read
|
|
69
45
|
// additionalData / launchURL and call getNotification() and display(), so one handler fits both SDKs.
|
|
70
46
|
function buildNotification(raw, display) {
|
|
71
47
|
const payload = raw || {};
|
|
72
|
-
const data =
|
|
48
|
+
const data = payload.data || {};
|
|
49
|
+
const buttons = Array.isArray(payload.buttons) ? payload.buttons : [];
|
|
73
50
|
return {
|
|
74
51
|
...payload,
|
|
75
52
|
notificationId: payload.notificationId ?? null,
|
|
@@ -78,6 +55,12 @@ function buildNotification(raw, display) {
|
|
|
78
55
|
launchURL: payload.url ?? null,
|
|
79
56
|
additionalData: data,
|
|
80
57
|
data,
|
|
58
|
+
// OneSignal's names for things Pushbib calls something else, so a handler written
|
|
59
|
+
// against it reads straight across without a branch per provider.
|
|
60
|
+
actionButtons: buttons,
|
|
61
|
+
bigPicture: payload.imageUrl ?? null,
|
|
62
|
+
groupKey: payload.collapseId ?? null,
|
|
63
|
+
rawPayload: payload,
|
|
81
64
|
display: display || (() => {}),
|
|
82
65
|
};
|
|
83
66
|
}
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const SDK_VERSION = '0.2.
|
|
1
|
+
export const SDK_VERSION = '0.2.1';
|
|
2
2
|
export const SDK_NAME = `pushbib-rn/${SDK_VERSION}`;
|