Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
#105 Firebase was unable to connect to FCM
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Aug 20, 2016
1 parent 1c17507 commit fd80ddb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<img src="docs/images/firebase-logo.png" width="116px" height="32px" alt="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)
Expand Down
64 changes: 35 additions & 29 deletions firebase.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
});
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion platforms/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

pod 'Firebase', '~> 3.3.0'
pod 'Firebase', '~> 3.4.0'
pod 'Firebase/Database'
pod 'Firebase/Auth'
pod 'Firebase/Crash'
Expand Down

0 comments on commit fd80ddb

Please sign in to comment.