From fd80ddb585811645ca09c8857596c639ddbf8307 Mon Sep 17 00:00:00 2001 From: Eddy Verbruggen Date: Sat, 20 Aug 2016 10:08:09 +0200 Subject: [PATCH] #105 Firebase was unable to connect to FCM --- CHANGELOG.md | 18 ++++++++++++ firebase.ios.js | 64 +++++++++++++++++++++++-------------------- package.json | 2 +- platforms/ios/Podfile | 2 +- 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0912d505..3583e4aa 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ Firebase +## 3.5.2 (2016, August 20) + +[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.1...3.5.2) + +### SDK versions +If version numbers changed clean your platform folders to avoid build errors. + +- __iOS: 3.4.x__ +- Android: 9.4.0 + +### New +- [#104](#104) Swap authentiction to a different Google account + +### Fixes +- [#105](#105) Receiving notifications from FCM on iOS may work better now + + + ## 3.5.1 (2016, August 12) [Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.0...3.5.1) diff --git a/firebase.ios.js b/firebase.ios.js index 1fe1684d..5a64157b 100755 --- a/firebase.ios.js +++ b/firebase.ios.js @@ -57,6 +57,16 @@ firebase.addAppDelegateMethods = function(appDelegate) { // making this conditional to avoid http://stackoverflow.com/questions/37428539/firebase-causes-issue-missing-push-notification-entitlement-after-delivery-to ? if (typeof(FIRMessaging) !== "undefined") { + appDelegate.prototype.applicationDidRegisterForRemoteNotificationsWithDeviceToken = function (application, devToken) { + // TODO guard with _messagingConnected ? + FIRInstanceID.instanceID().setAPNSTokenType(devToken, FIRInstanceIDAPNSTokenTypeUnknown); + FIRMessaging.messaging().connectWithCompletion(function(error) { + if (!error) { + firebase._messagingConnected = true; + } + }); + }; + appDelegate.prototype.applicationDidReceiveRemoteNotificationFetchCompletionHandler = function (application, userInfo, completionHandler) { completionHandler(UIBackgroundFetchResultNewData); var userInfoJSON = firebase.toJsObject(userInfo); @@ -124,16 +134,39 @@ firebase._processPendingNotifications = function() { firebase._receivedNotificationCallback(userInfoJSON); } firebase._pendingNotifications = []; - firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification); UIApplication.sharedApplication().applicationIconBadgeNumber = 0; } }; +firebase._onTokenRefreshNotification = function (notification) { + var token = FIRInstanceID.instanceID().token(); + if (token === null) { + return; + } + + console.log("Firebase FCM token received: " + token); + + if (firebase._receivedPushTokenCallback) { + firebase._receivedPushTokenCallback(token); + } + + FIRMessaging.messaging().connectWithCompletion(function(error) { + if (error) { + // this is not fatal and it scares the hell out of ppl so not logging it + // console.log("Firebase was unable to connect to FCM. Error: " + error); + } else { + firebase._messagingConnected = true; + } + }); +}; + // rather than hijacking the appDelegate for these we'll be a good citizen and listen to the notifications (function () { if (typeof(FIRMessaging) !== "undefined") { + firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification); + firebase._addObserver(UIApplicationDidFinishLaunchingNotification, function (appNotification) { var notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationActivationModeBackground; var notificationSettings = UIUserNotificationSettings.settingsForTypesCategories(notificationTypes, null); @@ -161,9 +194,7 @@ firebase._processPendingNotifications = function() { // Firebase notifications (FCM) if (firebase._messagingConnected !== null) { FIRMessaging.messaging().connectWithCompletion(function(error) { - if (error) { - console.log("Firebase was unable to connect to FCM. Error: " + error); - } else { + if (!error) { firebase._messagingConnected = true; } }); @@ -308,12 +339,9 @@ firebase.init = function (arg) { // Firebase notifications (FCM) if (typeof(FIRMessaging) !== "undefined") { - firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification); - if (arg.onMessageReceivedCallback !== undefined) { firebase.addOnMessageReceivedCallback(arg.onMessageReceivedCallback); } - if (arg.onPushTokenReceivedCallback !== undefined) { firebase.addOnPushTokenReceivedCallback(arg.onPushTokenReceivedCallback); } @@ -336,28 +364,6 @@ firebase.init = function (arg) { }); }; -firebase._onTokenRefreshNotification = function (notification) { - var token = FIRInstanceID.instanceID().token(); - if (token === null) { - return; - } - - console.log("Firebase FCM token received: " + token); - - if (firebase._receivedPushTokenCallback) { - firebase._receivedPushTokenCallback(token); - } - - FIRMessaging.messaging().connectWithCompletion(function(error) { - if (error) { - // this is not fatal at all but still would like to know how often this happens - console.log("Firebase was unable to connect to FCM. Error: " + error); - } else { - firebase._messagingConnected = true; - } - }); -}; - firebase.getRemoteConfig = function (arg) { return new Promise(function (resolve, reject) { try { diff --git a/package.json b/package.json index 54cc38bb..3a6612e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-plugin-firebase", - "version": "3.5.2-dev", + "version": "3.5.2", "description" : "Fire. Base. Firebase!", "main" : "firebase.js", "nativescript": { diff --git a/platforms/ios/Podfile b/platforms/ios/Podfile index d1423ef2..3fc34883 100644 --- a/platforms/ios/Podfile +++ b/platforms/ios/Podfile @@ -1,5 +1,5 @@ -pod 'Firebase', '~> 3.3.0' +pod 'Firebase', '~> 3.4.0' pod 'Firebase/Database' pod 'Firebase/Auth' pod 'Firebase/Crash'