Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from rees46/fix/click-event
Browse files Browse the repository at this point in the history
Fix: click event
  • Loading branch information
TorinAsakura authored Feb 19, 2024
2 parents 56dfbd8 + 2920da1 commit e1ad5da
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
61 changes: 55 additions & 6 deletions MainSDK.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Linking } from 'react-native';
import {
initLocker,
request,
Expand All @@ -20,6 +21,39 @@ import {SDK_PUSH_CHANNEL} from "./index";
import Performer from './lib/performer';
import DeviceInfo from 'react-native-device-info';

/**
* @typedef {Object} Event
* @property {string} type
* @property {string} uri
*/

/**
* @typedef {Object} GoogleData
* @property {string} message_id
*/

/**
* @typedef {Object} Data
* @property {string} message_id
* @property {string} from
* @property {number} ttl
* @property {number} sentTime
* @property {string} id
* @property {string} type
* @property {GoogleData} google
*/

/**
* @typedef {Object} Notification
* @property {Data} [data]
* @property {boolean} userInteraction
* @property {boolean} foreground
* @property {string} channelId
* @property {string} id
* @property {string} message
* @property {string} title
*/

export var DEBUG = false;

class MainSDK extends Performer {
Expand Down Expand Up @@ -94,6 +128,11 @@ class MainSDK extends Performer {
pushBgReceivedListener = async function (remoteMessage) {
await this.showNotification(remoteMessage);
};
/**
*
* @param {Notification} remoteMessage
* @returns {Promise<void>}
*/
pushClickListener = async function (remoteMessage) {
await this.onClickPush(remoteMessage);
};
Expand Down Expand Up @@ -382,7 +421,7 @@ class MainSDK extends Performer {
await this.pushBgReceivedListener(remoteMessage);
});

// Subscribe to click notification
/** Subscribe to click notification */
PushNotification.configure({
popInitialNotification: true,
requestPermissions: true,
Expand Down Expand Up @@ -486,19 +525,29 @@ class MainSDK extends Performer {
}));
})
}

/**
* @param {Notification} [notification]
* @returns {Promise<*|boolean>}
*/
async onClickPush (notification) {
const messageId = notification.data.message_id || notification.data['google.message_id'];
let pushData = await getPushData(messageId);
if ( pushData.length === 0 || !pushData[0].data.event) {
return false;
}
if (pushData.length === 0) return false;

await removePushMessage(messageId);
this.notificationClicked({
code: notification?.data?.id,
type: notification?.data?.type
});
let message_event = JSON.parse(pushData[0].data.event),
message_url = '';

/** @type {string|undefined} */
const event = pushData[0].data.event;
if (!event) return false;

/** @type {Event|null} */
const message_event = JSON.parse(event);
let message_url = '';
switch (message_event.type) {
case "web":
message_url = message_event.uri;
Expand Down
5 changes: 5 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ export async function request(url, options = {}) {
return error;
});
}

/**
* @param {string|null} messageId
* @returns {Promise<*[]|Data[]>}
*/
export async function getPushData (messageId = null) {
try {
const pushData = JSON.parse(await AsyncStorage.getItem(SDK_STORAGE_NAME + '_push')) ?? [];
Expand Down

0 comments on commit e1ad5da

Please sign in to comment.