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

Commit 508280c

Browse files
Merge pull request #515 from Spenstar/master
[FIX] iOS registerForRemoteNotifications() Now Runs on Main Thread
2 parents 677ba39 + b7d6ace commit 508280c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

firebase.ios.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ firebase._gIDAuthentication = null;
1414
firebase._cachedInvitation = null;
1515
firebase._cachedDynamicLink = null;
1616

17+
/**
18+
* Workaround function to call the `dispatch_get_main_queue(...)` for iOS
19+
* thanks to Alexander Ziskind found on:
20+
* http://nuvious.com/Blog/2016/7/5/calling-dispatch_async-in-nativescript
21+
*/
22+
let invokeOnRunLoop = (function () {
23+
var runloop = CFRunLoopGetMain();
24+
return function (func) {
25+
CFRunLoopPerformBlock(runloop, kCFRunLoopDefaultMode, func);
26+
CFRunLoopWakeUp(runloop);
27+
}
28+
}());
29+
1730
firebase._addObserver = function (eventName, callback) {
1831
var queue = utils.ios.getter(NSOperationQueue, NSOperationQueue.mainQueue);
1932
return utils.ios.getter(NSNotificationCenter, NSNotificationCenter.defaultCenter).addObserverForNameObjectQueueUsingBlock(eventName, null, queue, callback);
@@ -343,7 +356,9 @@ firebase._registerForRemoteNotifications = function () {
343356
app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
344357
}
345358
if (app !== null) {
346-
app.registerForRemoteNotifications();
359+
invokeOnRunLoop(() => {
360+
app.registerForRemoteNotifications();
361+
});
347362
}
348363
} else {
349364
console.log("Error requesting push notification auth: " + error);
@@ -388,7 +403,9 @@ firebase._registerForRemoteNotifications = function () {
388403
} else {
389404
var notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationActivationModeBackground;
390405
var notificationSettings = UIUserNotificationSettings.settingsForTypesCategories(notificationTypes, null);
391-
app.registerForRemoteNotifications(); // prompts the user to accept notifications
406+
invokeOnRunLoop(() => {
407+
app.registerForRemoteNotifications(); // prompts the user to accept notifications
408+
});
392409
app.registerUserNotificationSettings(notificationSettings);
393410
}
394411
};

0 commit comments

Comments
 (0)