import { Dialogs } from "@nativescript/core" ; import { firebase } from "@nativescript/firebase" ; import { messaging } from "@nativescript/firebase/messaging" ; const getCircularReplacer = ( ) => { const seen = new WeakSet ; return ( key , value ) => { if ( typeof value === "object" && value !== null ) { if ( seen . has ( value ) ) { return ; } seen . add ( value ) ; } return value ; } ; } ; export class MessagingViewModel { public doGetCurrentPushToken ( ) { firebase . getCurrentPushToken ( ) . then ( token => { console . log ( "Current push token: " + token ) ; Dialogs . alert ( { title : "Current Push Token" , message : ( token === null ? "Not received yet" : token + ( "\n\nSee the console log if you want to copy-paste it." ) ) , okButtonText : "OK, thx" } ) ; } ) ; } public doRegisterForInteractivePush ( ) { if ( global . isIOS ) { const model = new messaging . PushNotificationModel ( ) ; model . iosSettings = new messaging . IosPushSettings ( ) ; model . iosSettings . badge = false ; model . iosSettings . alert = true ; model . iosSettings . interactiveSettings = new messaging . IosInteractivePushSettings ( ) ; model . iosSettings . interactiveSettings . actions = [ { identifier : "OPEN_ACTION" , title : "Open the app (if closed)" , options : messaging . IosInteractiveNotificationActionOptions . foreground } , { identifier : "AUTH" , title : "Open the app, but only if device is not locked with a passcode" , options : messaging . IosInteractiveNotificationActionOptions . foreground | messaging . IosInteractiveNotificationActionOptions . authenticationRequired } , { identifier : "INPUT_ACTION" , title : "Tap to reply without opening the app" , type : "input" , submitLabel : "Fire!" , placeholder : "Load the gun..." } , { identifier : "INPUT_ACTION" , title : "Tap to reply and open the app" , options : messaging . IosInteractiveNotificationActionOptions . foreground , type : "input" , submitLabel : "OK, send it" , placeholder : "Type here, baby!" } , { identifier : "DELETE_ACTION" , title : "Delete without opening the app" , options : messaging . IosInteractiveNotificationActionOptions . destructive } ] ; model . iosSettings . interactiveSettings . categories = [ { identifier : "GENERAL" } ] ; model . onNotificationActionTakenCallback = ( actionIdentifier , message , inputText ? ) => { console . log ( `onNotificationActionTakenCallback fired! \n\r Message: ${ JSON . stringify ( message ) }, \n\r Action taken: ${ actionIdentifier }` ) ; alert ( { title : "Interactive push action" , message : `Message: ${ JSON . stringify ( message ) }, \n\r Action taken: ${ actionIdentifier }` + ( inputText ? `, \n\r Input text: ${ inputText }` : "" ) , okButtonText : "Nice!" } ) ; } ; firebase . registerForInteractivePush ( model ) ; console . log ( "Registered for interactive push" ) ; } else { console . log ( "Interactive push messaging is iOS-only!" ) ; } } public doRegisterPushHandlers ( ) { firebase . addOnPushTokenReceivedCallback ( token => { console . log ( "Firebase plugin received a push token: " + token ) ; } ) ; firebase . addOnMessageReceivedCallback ( message => { console . log ( "------------------- push message received: " + JSON . stringify ( message , getCircularReplacer ( ) ) ) ; setTimeout ( ( ) => { alert ( { title : "Push message!" , message : ( message . title !== undefined ? message . title : "" ) , okButtonText : "Sw33t" } ) ; } , 500 ) ; } ) . then ( ( ) => { console . log ( "Added addOnMessageReceivedCallback" ) ; } , err => { console . log ( "Failed to add addOnMessageReceivedCallback: " + err ) ; } ) ; } public doUnregisterForPushNotifications ( ) { firebase . unregisterForPushNotifications ( ) . then ( ( ) => { alert ( { title : "Unregistered" , message : "If you were registered, that is." , okButtonText : "Got it, thanks!" } ) ; } ) ; } public doSubscribeToTopic ( ) { firebase . subscribeToTopic ( "demo" ) . then ( ( ) => { alert ( { title : "Subscribed" , message : ".. to the 'demo' topic" , okButtonText : "Okay, interesting" } ) ; } , error => { alert ( { title : "Subscribe error" , message : error , okButtonText : "OK" } ) ; } ) ; } public doUnsubscribeFromTopic ( ) { firebase . unsubscribeFromTopic ( "demo" ) . then ( ( ) => { alert ( { title : "Unsubscribed" , message : ".. from the 'demo' topic" , okButtonText : "Okay, very interesting" } ) ; } , error => { alert ( { title : "Unsubscribe error" , message : error , okButtonText : "OK" } ) ; } ) ; } public doGetAreNotificationsEnabled ( ) { alert ( { title : "AreNotificationsEnabled" , message : "" + firebase . areNotificationsEnabled ( ) , okButtonText : "Okay, very interesting" } ) ; } }