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

Commit 62dac70

Browse files
committed
Let iOS handle push notifications after the app was killed in the springboard
1 parent 858d18e commit 62dac70

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

firebase.ios.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,38 @@ firebase._addObserver = function (eventName, callback) {
1717

1818
firebase.addAppDelegateMethods = function(appDelegate) {
1919

20+
function handleRemoteNotification(app, userInfo) {
21+
var userInfoJSON = firebase.toJsObject(userInfo);
22+
var aps = userInfo.objectForKey("aps");
23+
if (aps !== null) {
24+
var alert = aps.objectForKey("alert");
25+
if (alert !== null && alert.objectForKey) {
26+
userInfoJSON.title = alert.objectForKey("title");
27+
userInfoJSON.body = alert.objectForKey("body");
28+
}
29+
}
30+
31+
firebase._pendingNotifications.push(userInfoJSON);
32+
if (app.applicationState === UIApplicationState.UIApplicationStateActive) {
33+
// If this is called from applicationDidFinishLaunchingWithOptions probably the app was dead (background)
34+
userInfoJSON.foreground = true;
35+
if (firebase._receivedNotificationCallback !== null) {
36+
firebase._processPendingNotifications();
37+
}
38+
} else {
39+
userInfoJSON.foreground = false;
40+
}
41+
}
42+
2043
// we need the launchOptions for this one so it's a bit hard to use the UIApplicationDidFinishLaunchingNotification pattern we're using for other things
2144
appDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {
45+
// If the app was terminated and the iOS is launching it in result of push notification tapped by the user, this will hold the notification data.
46+
if (launchOptions && typeof(FIRMessaging) !== "undefined") {
47+
var remoteNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey);
48+
if (remoteNotification) {
49+
handleRemoteNotification(application, remoteNotification);
50+
}
51+
}
2252
// Firebase Facebook authentication
2353
if (typeof(FBSDKApplicationDelegate) !== "undefined") {
2454
FBSDKApplicationDelegate.sharedInstance().applicationDidFinishLaunchingWithOptions(application, launchOptions);
@@ -74,25 +104,7 @@ firebase.addAppDelegateMethods = function(appDelegate) {
74104

75105
appDelegate.prototype.applicationDidReceiveRemoteNotificationFetchCompletionHandler = function (app, userInfo, completionHandler) {
76106
completionHandler(UIBackgroundFetchResultNewData);
77-
var userInfoJSON = firebase.toJsObject(userInfo);
78-
var aps = userInfo.objectForKey("aps");
79-
if (aps !== null) {
80-
var alert = aps.objectForKey("alert");
81-
if (alert !== null && alert.objectForKey) {
82-
userInfoJSON.title = alert.objectForKey("title");
83-
userInfoJSON.body = alert.objectForKey("body");
84-
}
85-
}
86-
87-
firebase._pendingNotifications.push(userInfoJSON);
88-
if (app.applicationState === UIApplicationState.UIApplicationStateActive) {
89-
userInfoJSON.foreground = true;
90-
if (firebase._receivedNotificationCallback !== null) {
91-
firebase._processPendingNotifications();
92-
}
93-
} else {
94-
userInfoJSON.foreground = false;
95-
}
107+
handleRemoteNotification(app, userInfo);
96108
};
97109
}
98110
};
@@ -161,6 +173,13 @@ firebase.unregisterForPushNotifications = function (callback) {
161173
};
162174

163175
firebase._processPendingNotifications = function() {
176+
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
177+
if (!app) {
178+
application.on("launch", function() {
179+
firebase._processPendingNotifications();
180+
});
181+
return;
182+
}
164183
if (firebase._receivedNotificationCallback !== null) {
165184
for (var p in firebase._pendingNotifications) {
166185
var userInfoJSON = firebase._pendingNotifications[p];
@@ -174,7 +193,7 @@ firebase._processPendingNotifications = function() {
174193
firebase._receivedNotificationCallback(userInfoJSON);
175194
}
176195
firebase._pendingNotifications = [];
177-
utils.ios.getter(UIApplication, UIApplication.sharedApplication).applicationIconBadgeNumber = 0;
196+
app.applicationIconBadgeNumber = 0;
178197
}
179198
};
180199

@@ -203,6 +222,13 @@ firebase._onTokenRefreshNotification = function (notification) {
203222
firebase._registerForRemoteNotificationsRanThisSession = false;
204223

205224
firebase._registerForRemoteNotifications = function (app) {
225+
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
226+
if (!app) {
227+
application.on("launch", function() {
228+
firebase._registerForRemoteNotifications();
229+
});
230+
return;
231+
}
206232
if (firebase._registerForRemoteNotificationsRanThisSession) {
207233
// ignore
208234
return;

0 commit comments

Comments
 (0)