|
5438 | 5438 | return base.PubNubFile; |
5439 | 5439 | }, |
5440 | 5440 | get version() { |
5441 | | - return '10.2.7'; |
| 5441 | + return '10.2.8'; |
5442 | 5442 | }, |
5443 | 5443 | getVersion() { |
5444 | 5444 | return this.version; |
|
7555 | 7555 | } |
7556 | 7556 | } |
7557 | 7557 |
|
7558 | | - // -------------------------------------------------------- |
7559 | | - // ------------------------ Types ------------------------- |
7560 | | - // -------------------------------------------------------- |
7561 | | - // region Types |
7562 | | - // endregion |
7563 | 7558 | // endregion |
7564 | 7559 | /** |
7565 | 7560 | * Base notification payload object. |
|
7897 | 7892 | get data() { |
7898 | 7893 | return this.payload.data; |
7899 | 7894 | } |
| 7895 | + /** |
| 7896 | + * Retrieve Android notification payload and initialize required structure. |
| 7897 | + * |
| 7898 | + * @returns Android specific notification payload. |
| 7899 | + */ |
| 7900 | + get androidNotification() { |
| 7901 | + var _c; |
| 7902 | + return (_c = this.payload.android) === null || _c === void 0 ? void 0 : _c.notification; |
| 7903 | + } |
7900 | 7904 | /** |
7901 | 7905 | * Notification title. |
7902 | 7906 | * |
|
7914 | 7918 | if (!value || !value.length) |
7915 | 7919 | return; |
7916 | 7920 | this.payload.notification.title = value; |
| 7921 | + this.androidNotification.title = value; |
7917 | 7922 | this._title = value; |
7918 | 7923 | } |
7919 | 7924 | /** |
|
7933 | 7938 | if (!value || !value.length) |
7934 | 7939 | return; |
7935 | 7940 | this.payload.notification.body = value; |
| 7941 | + this.androidNotification.body = value; |
7936 | 7942 | this._body = value; |
7937 | 7943 | } |
| 7944 | + /** |
| 7945 | + * Retrieve unread notifications number. |
| 7946 | + * |
| 7947 | + * @returns Number of unread notifications which should be shown on application badge. |
| 7948 | + */ |
| 7949 | + get badge() { |
| 7950 | + return this._badge; |
| 7951 | + } |
| 7952 | + /** |
| 7953 | + * Update application badge number. |
| 7954 | + * |
| 7955 | + * @param value - Number which should be shown in application badge upon receiving notification. |
| 7956 | + */ |
| 7957 | + set badge(value) { |
| 7958 | + if (value === undefined || value === null) |
| 7959 | + return; |
| 7960 | + this.androidNotification.notification_count = value; |
| 7961 | + this._badge = value; |
| 7962 | + } |
7938 | 7963 | /** |
7939 | 7964 | * Retrieve notification sound file. |
7940 | 7965 | * |
|
7951 | 7976 | set sound(value) { |
7952 | 7977 | if (!value || !value.length) |
7953 | 7978 | return; |
7954 | | - this.payload.notification.sound = value; |
| 7979 | + this.androidNotification.sound = value; |
7955 | 7980 | this._sound = value; |
7956 | 7981 | } |
7957 | 7982 | /** |
|
7965 | 7990 | /** |
7966 | 7991 | * Update notification icon. |
7967 | 7992 | * |
7968 | | - * @param value - Name of the icon file which should be shown on notification. |
| 7993 | + * @param value - Name of the icon file set for `android.notification.icon`. |
7969 | 7994 | */ |
7970 | 7995 | set icon(value) { |
7971 | 7996 | if (!value || !value.length) |
7972 | 7997 | return; |
7973 | | - this.payload.notification.icon = value; |
| 7998 | + this.androidNotification.icon = value; |
7974 | 7999 | this._icon = value; |
7975 | 8000 | } |
7976 | 8001 | /** |
|
7984 | 8009 | /** |
7985 | 8010 | * Update notifications grouping tag. |
7986 | 8011 | * |
7987 | | - * @param value - String which will be used to group similar notifications in notification center. |
| 8012 | + * @param value - String set for `android.notification.tag` to group similar notifications. |
7988 | 8013 | */ |
7989 | 8014 | set tag(value) { |
7990 | 8015 | if (!value || !value.length) |
7991 | 8016 | return; |
7992 | | - this.payload.notification.tag = value; |
| 8017 | + this.androidNotification.tag = value; |
7993 | 8018 | this._tag = value; |
7994 | 8019 | } |
7995 | 8020 | /** |
|
8010 | 8035 | setDefaultPayloadStructure() { |
8011 | 8036 | this.payload.notification = {}; |
8012 | 8037 | this.payload.data = {}; |
| 8038 | + this.payload.android = { notification: {} }; |
8013 | 8039 | } |
8014 | 8040 | /** |
8015 | 8041 | * Translate data object into PubNub push notification payload object. |
|
8019 | 8045 | * @returns Preformatted push notification payload. |
8020 | 8046 | */ |
8021 | 8047 | toObject() { |
8022 | | - let data = Object.assign({}, this.payload.data); |
8023 | | - let notification = null; |
| 8048 | + var _c, _e; |
8024 | 8049 | const payload = {}; |
8025 | | - // Check whether additional data has been passed outside 'data' object and put it into it if required. |
8026 | | - if (Object.keys(this.payload).length > 2) { |
8027 | | - const _a = this.payload, additionalData = __rest(_a, ["notification", "data"]); |
8028 | | - data = Object.assign(Object.assign({}, data), additionalData); |
| 8050 | + const notification = Object.assign({}, this.payload.notification); |
| 8051 | + const android = Object.assign({}, this.payload.android); |
| 8052 | + const androidNotification = Object.assign({}, ((_c = android.notification) !== null && _c !== void 0 ? _c : {})); |
| 8053 | + // Strip title/body from android.notification — they belong in top-level notification (or data for silent). |
| 8054 | + const androidSpecificFields = __rest(androidNotification, ["title", "body"]); |
| 8055 | + if (this._isSilent) { |
| 8056 | + // For silent (data-only) notifications, strip all `notification` fields |
| 8057 | + // (both root and android) and move everything into the root `data` object. |
| 8058 | + const data = {}; |
| 8059 | + if (this._title) |
| 8060 | + data.title = this._title; |
| 8061 | + if (this._body) |
| 8062 | + data.body = this._body; |
| 8063 | + // Merge android-specific notification fields (sound, icon, tag, etc.) into data. |
| 8064 | + for (const [key, value] of Object.entries(androidSpecificFields)) { |
| 8065 | + if (value !== undefined && value !== null) |
| 8066 | + data[key] = String(value); |
| 8067 | + } |
| 8068 | + // Merge any existing user-provided custom data. |
| 8069 | + if (this.payload.data) |
| 8070 | + Object.assign(data, this.payload.data); |
| 8071 | + if (Object.keys(data).length) |
| 8072 | + payload.data = data; |
| 8073 | + // Exclude `notification` entirely from android — only keep non-notification android fields. |
| 8074 | + delete android.notification; |
| 8075 | + if (Object.keys(android).length) { |
| 8076 | + payload.android = android; |
| 8077 | + } |
8029 | 8078 | } |
8030 | | - if (this._isSilent) |
8031 | | - data.notification = this.payload.notification; |
8032 | | - else |
8033 | | - notification = this.payload.notification; |
8034 | | - if (Object.keys(data).length) |
8035 | | - payload.data = data; |
8036 | | - if (notification && Object.keys(notification).length) |
8037 | | - payload.notification = notification; |
| 8079 | + else { |
| 8080 | + if (Object.keys(notification).length) |
| 8081 | + payload.notification = notification; |
| 8082 | + // Include top-level data if present. |
| 8083 | + if (this.payload.data && Object.keys(this.payload.data).length) |
| 8084 | + payload.data = Object.assign({}, this.payload.data); |
| 8085 | + // android.notification should only contain android-specific fields (sound, icon, tag, etc.). |
| 8086 | + if (Object.keys(androidSpecificFields).length) { |
| 8087 | + const androidWithoutNotification = __rest(android, ["notification"]); |
| 8088 | + payload.android = Object.assign(Object.assign({}, androidWithoutNotification), { notification: androidSpecificFields }); |
| 8089 | + } |
| 8090 | + else { |
| 8091 | + const androidWithoutNotification = __rest(android, ["notification"]); |
| 8092 | + if (Object.keys(androidWithoutNotification).length) { |
| 8093 | + payload.android = androidWithoutNotification; |
| 8094 | + } |
| 8095 | + } |
| 8096 | + } |
| 8097 | + // Preserve other top-level FCM fields (apns, webpush, fcm_options, etc.). |
| 8098 | + const _f = this.payload, otherFields = __rest(_f, ["notification", "android", "data", "pn_exceptions"]); |
| 8099 | + Object.assign(payload, otherFields); |
| 8100 | + if ((_e = this.payload.pn_exceptions) === null || _e === void 0 ? void 0 : _e.length) |
| 8101 | + payload.pn_exceptions = this.payload.pn_exceptions; |
8038 | 8102 | return Object.keys(payload).length ? payload : null; |
8039 | 8103 | } |
8040 | 8104 | } |
|
0 commit comments