@@ -57,6 +57,16 @@ firebase.addAppDelegateMethods = function(appDelegate) {
5757
5858 // making this conditional to avoid http://stackoverflow.com/questions/37428539/firebase-causes-issue-missing-push-notification-entitlement-after-delivery-to ?
5959 if ( typeof ( FIRMessaging ) !== "undefined" ) {
60+ appDelegate . prototype . applicationDidRegisterForRemoteNotificationsWithDeviceToken = function ( application , devToken ) {
61+ // TODO guard with _messagingConnected ?
62+ FIRInstanceID . instanceID ( ) . setAPNSTokenType ( devToken , FIRInstanceIDAPNSTokenTypeUnknown ) ;
63+ FIRMessaging . messaging ( ) . connectWithCompletion ( function ( error ) {
64+ if ( ! error ) {
65+ firebase . _messagingConnected = true ;
66+ }
67+ } ) ;
68+ } ;
69+
6070 appDelegate . prototype . applicationDidReceiveRemoteNotificationFetchCompletionHandler = function ( application , userInfo , completionHandler ) {
6171 completionHandler ( UIBackgroundFetchResultNewData ) ;
6272 var userInfoJSON = firebase . toJsObject ( userInfo ) ;
@@ -124,16 +134,39 @@ firebase._processPendingNotifications = function() {
124134 firebase . _receivedNotificationCallback ( userInfoJSON ) ;
125135 }
126136 firebase . _pendingNotifications = [ ] ;
127- firebase . _addObserver ( kFIRInstanceIDTokenRefreshNotification , firebase . _onTokenRefreshNotification ) ;
128137 UIApplication . sharedApplication ( ) . applicationIconBadgeNumber = 0 ;
129138 }
130139} ;
131140
141+ firebase . _onTokenRefreshNotification = function ( notification ) {
142+ var token = FIRInstanceID . instanceID ( ) . token ( ) ;
143+ if ( token === null ) {
144+ return ;
145+ }
146+
147+ console . log ( "Firebase FCM token received: " + token ) ;
148+
149+ if ( firebase . _receivedPushTokenCallback ) {
150+ firebase . _receivedPushTokenCallback ( token ) ;
151+ }
152+
153+ FIRMessaging . messaging ( ) . connectWithCompletion ( function ( error ) {
154+ if ( error ) {
155+ // this is not fatal and it scares the hell out of ppl so not logging it
156+ // console.log("Firebase was unable to connect to FCM. Error: " + error);
157+ } else {
158+ firebase . _messagingConnected = true ;
159+ }
160+ } ) ;
161+ } ;
162+
132163// rather than hijacking the appDelegate for these we'll be a good citizen and listen to the notifications
133164( function ( ) {
134165
135166 if ( typeof ( FIRMessaging ) !== "undefined" ) {
136167
168+ firebase . _addObserver ( kFIRInstanceIDTokenRefreshNotification , firebase . _onTokenRefreshNotification ) ;
169+
137170 firebase . _addObserver ( UIApplicationDidFinishLaunchingNotification , function ( appNotification ) {
138171 var notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationActivationModeBackground ;
139172 var notificationSettings = UIUserNotificationSettings . settingsForTypesCategories ( notificationTypes , null ) ;
@@ -161,9 +194,7 @@ firebase._processPendingNotifications = function() {
161194 // Firebase notifications (FCM)
162195 if ( firebase . _messagingConnected !== null ) {
163196 FIRMessaging . messaging ( ) . connectWithCompletion ( function ( error ) {
164- if ( error ) {
165- console . log ( "Firebase was unable to connect to FCM. Error: " + error ) ;
166- } else {
197+ if ( ! error ) {
167198 firebase . _messagingConnected = true ;
168199 }
169200 } ) ;
@@ -308,12 +339,9 @@ firebase.init = function (arg) {
308339
309340 // Firebase notifications (FCM)
310341 if ( typeof ( FIRMessaging ) !== "undefined" ) {
311- firebase . _addObserver ( kFIRInstanceIDTokenRefreshNotification , firebase . _onTokenRefreshNotification ) ;
312-
313342 if ( arg . onMessageReceivedCallback !== undefined ) {
314343 firebase . addOnMessageReceivedCallback ( arg . onMessageReceivedCallback ) ;
315344 }
316-
317345 if ( arg . onPushTokenReceivedCallback !== undefined ) {
318346 firebase . addOnPushTokenReceivedCallback ( arg . onPushTokenReceivedCallback ) ;
319347 }
@@ -336,28 +364,6 @@ firebase.init = function (arg) {
336364 } ) ;
337365} ;
338366
339- firebase . _onTokenRefreshNotification = function ( notification ) {
340- var token = FIRInstanceID . instanceID ( ) . token ( ) ;
341- if ( token === null ) {
342- return ;
343- }
344-
345- console . log ( "Firebase FCM token received: " + token ) ;
346-
347- if ( firebase . _receivedPushTokenCallback ) {
348- firebase . _receivedPushTokenCallback ( token ) ;
349- }
350-
351- FIRMessaging . messaging ( ) . connectWithCompletion ( function ( error ) {
352- if ( error ) {
353- // this is not fatal at all but still would like to know how often this happens
354- console . log ( "Firebase was unable to connect to FCM. Error: " + error ) ;
355- } else {
356- firebase . _messagingConnected = true ;
357- }
358- } ) ;
359- } ;
360-
361367firebase . getRemoteConfig = function ( arg ) {
362368 return new Promise ( function ( resolve , reject ) {
363369 try {
0 commit comments