@amrshbib/react-native-pushbib 0.1.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 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: Map<String, String>,
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", JSONObject(data as Map<*, *>))
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
- val custom = HashMap<String, String>()
66
- for ((k, v) in data) if (k !in CONTROL_KEYS) custom[k] = v
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")?.let { obj ->
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, 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
- data: Record<string, string>;
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
- NSSet *controlKeys = [NSSet setWithArray:@[
116
- @"aps", @"url", @"imageUrl", @"largeIcon", @"buttons", @"pushbib_payload",
117
- @"showForeground", @"showBackground"
118
- ]];
119
-
120
- NSMutableDictionary *data = [NSMutableDictionary new];
121
- for (NSString *key in userInfo) {
122
- if (![key isKindOfClass:NSString.class]) continue;
123
- if ([controlKeys containsObject:key]) continue;
124
- if ([key hasPrefix:@"gcm."] || [key hasPrefix:@"google."]) continue;
125
- data[key] = userInfo[key];
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@amrshbib/react-native-pushbib",
3
- "version": "0.1.0",
4
- "description": "Self-hosted push notification SDK for React Native. Pushbib-style API (login, tags, click events) with zero Firebase setup in the app — no google-services.json, no GoogleService-Info.plist.",
3
+ "version": "0.3.0",
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",
7
7
  "react-native": "src/index.js",
package/src/index.js CHANGED
@@ -46,6 +46,7 @@ function tagItems(value) {
46
46
  function buildNotification(raw, display) {
47
47
  const payload = raw || {};
48
48
  const data = payload.data || {};
49
+ const buttons = Array.isArray(payload.buttons) ? payload.buttons : [];
49
50
  return {
50
51
  ...payload,
51
52
  notificationId: payload.notificationId ?? null,
@@ -54,6 +55,12 @@ function buildNotification(raw, display) {
54
55
  launchURL: payload.url ?? null,
55
56
  additionalData: data,
56
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,
57
64
  display: display || (() => {}),
58
65
  };
59
66
  }
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export const SDK_VERSION = '0.1.0';
1
+ export const SDK_VERSION = '0.2.1';
2
2
  export const SDK_NAME = `pushbib-rn/${SDK_VERSION}`;