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

Commit f193b66

Browse files
Merge pull request #222 from PanayotCankov/master
Let iOS handle push notifications after the app was killed in the springboard
2 parents 858d18e + 6c1e4f3 commit f193b66

File tree

3 files changed

+110
-80
lines changed

3 files changed

+110
-80
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;

scripts/install_ios_entitlements.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,58 @@ var xcode = require('xcode'),
33
path = require('path'),
44
pjson = eval('require(\'../../package.json\')'),
55
iosFolder = path.join('platforms', 'ios'),
6-
data = fs.readdirSync(iosFolder),
6+
data = fs.existsSync(iosFolder) && fs.readdirSync(iosFolder),
77
projFolder,
88
projName;
99

1010
// Find the project folder by looking for *.xcodeproj
11-
if (data && data.length) {
11+
if (!(data && data.length)) {
12+
console.log("platforms/ios does not exist, skip copying entitlements.");
13+
} else {
1214
data.forEach(function (folder) {
1315
if (folder.match(/\.xcodeproj$/)) {
1416
projFolder = path.join(iosFolder, folder);
1517
projName = path.basename(folder, '.xcodeproj');
1618
}
1719
});
18-
}
1920

20-
if (!projFolder || !projName) {
21-
throw new Error("Could not find an .xcodeproj folder in: " + iosFolder);
22-
}
21+
if (!projFolder || !projName) {
22+
throw new Error("Could not find an .xcodeproj folder in: " + iosFolder);
23+
}
2324

24-
var destFolder = path.join(iosFolder, projName, 'Resources');
25-
if (!fs.existsSync(destFolder)) {
26-
fs.mkdirSync(destFolder);
27-
}
25+
var destFolder = path.join(iosFolder, projName, 'Resources');
26+
if (!fs.existsSync(destFolder)) {
27+
fs.mkdirSync(destFolder);
28+
}
2829

29-
var destFile = path.join(destFolder, projName + '.entitlements');
30+
var destFile = path.join(destFolder, projName + '.entitlements');
3031

31-
if (!fs.existsSync(destFile)) {
32-
var bundleID = pjson.nativescript.id;
32+
if (!fs.existsSync(destFile)) {
33+
var bundleID = pjson.nativescript.id;
3334

34-
// create a new entitlements plist file
35-
var sourceFile = path.join('node_modules', 'nativescript-plugin-firebase', 'scripts', 'resources', 'KeychainSharing.entitlements');
35+
// create a new entitlements plist file
36+
var sourceFile = path.join('node_modules', 'nativescript-plugin-firebase', 'scripts', 'resources', 'KeychainSharing.entitlements');
3637

37-
var fileData = fs.readFileSync(sourceFile).toString();
38-
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
39-
fs.writeFileSync(destFile, fileData);
38+
var fileData = fs.readFileSync(sourceFile).toString();
39+
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
40+
fs.writeFileSync(destFile, fileData);
4041

41-
var projectPath = path.join(projFolder, 'project.pbxproj'),
42-
pbxProject = xcode.project(projectPath);
42+
var projectPath = path.join(projFolder, 'project.pbxproj'),
43+
pbxProject = xcode.project(projectPath);
4344

44-
pbxProject.parseSync();
45-
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
45+
pbxProject.parseSync();
46+
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
4647

4748

48-
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
49-
for (var key in configGroups) {
50-
var config = configGroups[key];
51-
if (config.buildSettings !== undefined) {
52-
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
49+
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
50+
for (var key in configGroups) {
51+
var config = configGroups[key];
52+
if (config.buildSettings !== undefined) {
53+
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
54+
}
5355
}
54-
}
5556

56-
// write the updated project file
57-
fs.writeFileSync(projectPath, pbxProject.writeSync());
57+
// write the updated project file
58+
fs.writeFileSync(projectPath, pbxProject.writeSync());
59+
}
5860
}

scripts/install_ios_entitlements_packed.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,58 +3706,60 @@ var xcode = __webpack_require__(/*! xcode */ 5),
37063706
path = __webpack_require__(/*! path */ 0),
37073707
pjson = eval('require(\'../../package.json\')'),
37083708
iosFolder = path.join('platforms', 'ios'),
3709-
data = fs.readdirSync(iosFolder),
3709+
data = fs.existsSync(iosFolder) && fs.readdirSync(iosFolder),
37103710
projFolder,
37113711
projName;
37123712

37133713
// Find the project folder by looking for *.xcodeproj
3714-
if (data && data.length) {
3714+
if (!(data && data.length)) {
3715+
console.log("platforms/ios does not exist, skip copying entitlements.");
3716+
} else {
37153717
data.forEach(function (folder) {
37163718
if (folder.match(/\.xcodeproj$/)) {
37173719
projFolder = path.join(iosFolder, folder);
37183720
projName = path.basename(folder, '.xcodeproj');
37193721
}
37203722
});
3721-
}
37223723

3723-
if (!projFolder || !projName) {
3724-
throw new Error("Could not find an .xcodeproj folder in: " + iosFolder);
3725-
}
3724+
if (!projFolder || !projName) {
3725+
throw new Error("Could not find an .xcodeproj folder in: " + iosFolder);
3726+
}
37263727

3727-
var destFolder = path.join(iosFolder, projName, 'Resources');
3728-
if (!fs.existsSync(destFolder)) {
3729-
fs.mkdirSync(destFolder);
3730-
}
3728+
var destFolder = path.join(iosFolder, projName, 'Resources');
3729+
if (!fs.existsSync(destFolder)) {
3730+
fs.mkdirSync(destFolder);
3731+
}
37313732

3732-
var destFile = path.join(destFolder, projName + '.entitlements');
3733+
var destFile = path.join(destFolder, projName + '.entitlements');
37333734

3734-
if (!fs.existsSync(destFile)) {
3735-
var bundleID = pjson.nativescript.id;
3735+
if (!fs.existsSync(destFile)) {
3736+
var bundleID = pjson.nativescript.id;
37363737

3737-
// create a new entitlements plist file
3738-
var sourceFile = path.join('node_modules', 'nativescript-plugin-firebase', 'scripts', 'resources', 'KeychainSharing.entitlements');
3738+
// create a new entitlements plist file
3739+
var sourceFile = path.join('node_modules', 'nativescript-plugin-firebase', 'scripts', 'resources', 'KeychainSharing.entitlements');
37393740

3740-
var fileData = fs.readFileSync(sourceFile).toString();
3741-
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
3742-
fs.writeFileSync(destFile, fileData);
3741+
var fileData = fs.readFileSync(sourceFile).toString();
3742+
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
3743+
fs.writeFileSync(destFile, fileData);
37433744

3744-
var projectPath = path.join(projFolder, 'project.pbxproj'),
3745-
pbxProject = xcode.project(projectPath);
3745+
var projectPath = path.join(projFolder, 'project.pbxproj'),
3746+
pbxProject = xcode.project(projectPath);
37463747

3747-
pbxProject.parseSync();
3748-
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
3748+
pbxProject.parseSync();
3749+
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
37493750

37503751

3751-
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
3752-
for (var key in configGroups) {
3753-
var config = configGroups[key];
3754-
if (config.buildSettings !== undefined) {
3755-
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
3752+
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
3753+
for (var key in configGroups) {
3754+
var config = configGroups[key];
3755+
if (config.buildSettings !== undefined) {
3756+
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
3757+
}
37563758
}
3757-
}
37583759

3759-
// write the updated project file
3760-
fs.writeFileSync(projectPath, pbxProject.writeSync());
3760+
// write the updated project file
3761+
fs.writeFileSync(projectPath, pbxProject.writeSync());
3762+
}
37613763
}
37623764

37633765

0 commit comments

Comments
 (0)