11var appModule = require ( "application" ) ;
22var firebase = require ( "./firebase-common" ) ;
33
4+ firebase . _launchNotification = null ;
5+
6+ ( function ( ) {
7+ if ( typeof ( com . google . firebase . messaging ) === "undefined" ) {
8+ return ;
9+ }
10+ appModule . onLaunch = function ( intent ) {
11+ var extras = intent . getExtras ( ) ;
12+ if ( extras !== null ) {
13+ var result = {
14+ foreground : false
15+ } ;
16+
17+ var iterator = extras . keySet ( ) . iterator ( ) ;
18+ while ( iterator . hasNext ( ) ) {
19+ var key = iterator . next ( ) ;
20+ if ( key !== "from" && key !== "collapse_key" ) {
21+ result [ key ] = extras . get ( key ) ;
22+ }
23+ }
24+
25+ // in case this was a cold start we don't have the _receivedNotificationCallback yet
26+ if ( firebase . _receivedNotificationCallback === null ) {
27+ firebase . _launchNotification = result ;
28+ } else {
29+ // add a little delay just to make sure clients alerting this message will see it as the UI needs to settle
30+ setTimeout ( function ( ) {
31+ firebase . _receivedNotificationCallback ( result ) ;
32+ } ) ;
33+ }
34+ }
35+ } ;
36+
37+ } ) ( ) ;
38+
439firebase . toHashMap = function ( obj ) {
540 var node = new java . util . HashMap ( ) ;
641 for ( var property in obj ) {
@@ -134,13 +169,12 @@ firebase.init = function (arg) {
134169
135170 // Firebase notifications (FCM)
136171 if ( typeof ( com . google . firebase . messaging ) !== "undefined" ) {
137- console . log ( "--- has messaging!" ) ;
138- // TODO see iOS:
139- // firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification);
140172 if ( arg . onMessageReceivedCallback !== undefined ) {
141- console . log ( "--- adding messaging callback!" ) ;
142173 firebase . addOnMessageReceivedCallback ( arg . onMessageReceivedCallback ) ;
143174 }
175+ if ( arg . onPushTokenReceivedCallback !== undefined ) {
176+ firebase . addOnPushTokenReceivedCallback ( arg . onPushTokenReceivedCallback ) ;
177+ }
144178 }
145179
146180 resolve ( firebase . instance ) ;
@@ -160,7 +194,22 @@ firebase.addOnMessageReceivedCallback = function (callback) {
160194 }
161195
162196 firebase . _receivedNotificationCallback = callback ;
163- firebase . _processPendingNotifications ( ) ;
197+
198+ org . nativescript . plugins . firebase . FirebasePlugin . setOnNotificationReceivedCallback (
199+ new org . nativescript . plugins . firebase . FirebasePluginListener ( {
200+ success : function ( notification ) {
201+ console . log ( "---------- received notification: " + notification ) ;
202+ callback ( JSON . parse ( notification ) ) ;
203+ }
204+ } )
205+ ) ;
206+
207+ // if the app was launched from a notification, process it now
208+ if ( firebase . _launchNotification !== null ) {
209+ callback ( firebase . _launchNotification ) ;
210+ firebase . _launchNotification = null ;
211+ }
212+
164213 resolve ( ) ;
165214 } catch ( ex ) {
166215 console . log ( "Error in firebase.addOnMessageReceivedCallback: " + ex ) ;
@@ -169,6 +218,30 @@ firebase.addOnMessageReceivedCallback = function (callback) {
169218 } ) ;
170219} ;
171220
221+ firebase . addOnPushTokenReceivedCallback = function ( callback ) {
222+ return new Promise ( function ( resolve , reject ) {
223+ try {
224+ if ( typeof ( com . google . firebase . messaging ) === "undefined" ) {
225+ reject ( "Uncomment firebase-messaging in the plugin's include.gradle first" ) ;
226+ return ;
227+ }
228+
229+ org . nativescript . plugins . firebase . FirebasePlugin . setOnPushTokenReceivedCallback (
230+ new org . nativescript . plugins . firebase . FirebasePluginListener ( {
231+ success : function ( token ) {
232+ callback ( token ) ;
233+ }
234+ } )
235+ ) ;
236+
237+ resolve ( ) ;
238+ } catch ( ex ) {
239+ console . log ( "Error in firebase.addOnPushTokenReceivedCallback: " + ex ) ;
240+ reject ( ex ) ;
241+ }
242+ } ) ;
243+ } ;
244+
172245firebase . getRemoteConfigDefaults = function ( properties ) {
173246 var defaults = { } ;
174247 for ( var p in properties ) {
@@ -180,6 +253,13 @@ firebase.getRemoteConfigDefaults = function (properties) {
180253 return defaults ;
181254} ;
182255
256+ firebase . _isGooglePlayServicesAvailable = function ( ) {
257+ var context = appModule . android . foregroundActivity ;
258+ var playServiceStatusSuccess = com . google . android . gms . common . ConnectionResult . SUCCESS ; // 0
259+ var playServicesStatus = com . google . android . gms . common . GoogleApiAvailability . getInstance ( ) . isGooglePlayServicesAvailable ( context ) ;
260+ return playServicesStatus === playServiceStatusSuccess ;
261+ } ;
262+
183263firebase . getRemoteConfig = function ( arg ) {
184264 return new Promise ( function ( resolve , reject ) {
185265 try {
@@ -193,6 +273,11 @@ firebase.getRemoteConfig = function (arg) {
193273 return ;
194274 }
195275
276+ if ( ! firebase . _isGooglePlayServicesAvailable ( ) ) {
277+ reject ( "Google Play services is required for this feature, but not available on this device" ) ;
278+ return ;
279+ }
280+
196281 // Get a Remote Config object instance
197282 firebaseRemoteConfig = com . google . firebase . remoteconfig . FirebaseRemoteConfig . getInstance ( ) ;
198283
@@ -236,7 +321,6 @@ firebase.getRemoteConfig = function (arg) {
236321
237322 var onFailureListener = new com . google . android . gms . tasks . OnFailureListener ( {
238323 onFailure : function ( exception ) {
239- console . log ( "--- onFailureListener: " + exception ) ;
240324 if ( exception == "com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException" ) {
241325 returnMethod ( true ) ;
242326 } else {
@@ -323,6 +407,11 @@ function toLoginResult(user) {
323407firebase . login = function ( arg ) {
324408 return new Promise ( function ( resolve , reject ) {
325409 try {
410+ if ( ! firebase . _isGooglePlayServicesAvailable ( ) ) {
411+ reject ( "Google Play services is required for this feature, but not available on this device" ) ;
412+ return ;
413+ }
414+
326415 var firebaseAuth = com . google . firebase . auth . FirebaseAuth . getInstance ( ) ;
327416 var onCompleteListener = new com . google . android . gms . tasks . OnCompleteListener ( {
328417 onComplete : function ( task ) {
0 commit comments