@@ -6,6 +6,7 @@ import PamTracker from './PamTracker';
66import type { ConsentMessage } from './interface/consent_message' ;
77import { type AppAttentionStyle } from './interface/app_attention_style' ;
88import { Linking } from 'react-native' ;
9+ import type { PamPushMessage } from './interface/pam_push_message' ;
910
1011const LINKING_ERROR =
1112 `The package 'pam-react-native' doesn't seem to be linked. Make sure: \n\n` +
@@ -130,6 +131,96 @@ export class Pam {
130131 }
131132 }
132133
134+ static isPushNotiFromPam ( message : any ) {
135+ return message . data . hasOwnProperty ( 'pam' ) ;
136+ }
137+
138+ static convertToPamPushMessage ( message : any ) : PamPushMessage | undefined {
139+ if ( Pam . isPushNotiFromPam ( message ) ) {
140+ let data = message . data ;
141+ let pam : any = data . pam ;
142+ let payload : Record < string , any > ;
143+
144+ try {
145+ payload = JSON . parse ( pam ) ;
146+ } catch ( e ) {
147+ return undefined ;
148+ }
149+
150+ const flex = payload . flex ;
151+ const regExp = / s r c = " ( .* ?) " / ;
152+ const match = flex . match ( regExp ) ;
153+ const banner = match ?. [ 1 ] ?? '' ;
154+ const pixel = payload . pixel ?? '' ;
155+ const popupType = payload . popup_type ?? '' ;
156+ const url = payload . url ;
157+ const title = message . notification ?. title ?? '' ;
158+ const description = message . notification ?. body ?? '' ;
159+
160+ var item = {
161+ deliverID : '' ,
162+ pixel : pixel ,
163+ title : title ,
164+ description : description ,
165+ thumbnailUrl : banner ,
166+ flex : flex ,
167+ url : url ,
168+ popupType : popupType ,
169+ date : new Date ( ) ,
170+ isOpen : false ,
171+ data : payload ,
172+ } ;
173+
174+ return item ;
175+ }
176+ return undefined ;
177+ }
178+
179+ static async loadPushNotificationsFromCustomerID (
180+ customerID : string
181+ ) : Promise < PamPushMessage [ ] | null > {
182+ const contactID = Pam . shared ?. contactState ?. getContactId ( ) ;
183+ const database = Pam . shared ?. contactState ?. getDatabase ( ) ;
184+ if ( ! contactID || ! database ) {
185+ return null ;
186+ }
187+ return await Pam . pamApi . loadPushNotificationsFromCustomerID (
188+ database ,
189+ contactID ,
190+ customerID
191+ ) ;
192+ }
193+
194+ static async loadPushNotificationsFromMobile (
195+ mobileNumber : string
196+ ) : Promise < PamPushMessage [ ] | null > {
197+ const contactID = Pam . shared ?. contactState ?. getContactId ( ) ;
198+ const database = Pam . shared ?. contactState ?. getDatabase ( ) ;
199+ if ( ! contactID || ! database ) {
200+ return null ;
201+ }
202+ return await Pam . pamApi . loadPushNotificationsFromMobile (
203+ database ,
204+ contactID ,
205+ mobileNumber
206+ ) ;
207+ }
208+
209+ static async loadPushNotificationsFromEmail (
210+ email : string
211+ ) : Promise < PamPushMessage [ ] | null > {
212+ const contactID = Pam . shared ?. contactState ?. getContactId ( ) ;
213+ const database = Pam . shared ?. contactState ?. getDatabase ( ) ;
214+ if ( ! contactID || ! database ) {
215+ return null ;
216+ }
217+ return await Pam . pamApi . loadPushNotificationsFromEmail (
218+ database ,
219+ contactID ,
220+ email
221+ ) ;
222+ }
223+
133224 static async track ( event : string , payload : Record < string , any > ) {
134225 return await Pam . shared ?. track ( event , payload ) ;
135226 }
0 commit comments