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

Let iOS handle push notifications after the app was killed in the springboard #222

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions firebase.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,38 @@ firebase._addObserver = function (eventName, callback) {

firebase.addAppDelegateMethods = function(appDelegate) {

function handleRemoteNotification(app, userInfo) {
var userInfoJSON = firebase.toJsObject(userInfo);
var aps = userInfo.objectForKey("aps");
if (aps !== null) {
var alert = aps.objectForKey("alert");
if (alert !== null && alert.objectForKey) {
userInfoJSON.title = alert.objectForKey("title");
userInfoJSON.body = alert.objectForKey("body");
}
}

firebase._pendingNotifications.push(userInfoJSON);
if (app.applicationState === UIApplicationState.UIApplicationStateActive) {
// If this is called from applicationDidFinishLaunchingWithOptions probably the app was dead (background)
userInfoJSON.foreground = true;
if (firebase._receivedNotificationCallback !== null) {
firebase._processPendingNotifications();
}
} else {
userInfoJSON.foreground = false;
}
}

// we need the launchOptions for this one so it's a bit hard to use the UIApplicationDidFinishLaunchingNotification pattern we're using for other things
appDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {
// 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.
if (launchOptions && typeof(FIRMessaging) !== "undefined") {
var remoteNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey);
if (remoteNotification) {
handleRemoteNotification(application, remoteNotification);
}
}
// Firebase Facebook authentication
if (typeof(FBSDKApplicationDelegate) !== "undefined") {
FBSDKApplicationDelegate.sharedInstance().applicationDidFinishLaunchingWithOptions(application, launchOptions);
Expand Down Expand Up @@ -74,25 +104,7 @@ firebase.addAppDelegateMethods = function(appDelegate) {

appDelegate.prototype.applicationDidReceiveRemoteNotificationFetchCompletionHandler = function (app, userInfo, completionHandler) {
completionHandler(UIBackgroundFetchResultNewData);
var userInfoJSON = firebase.toJsObject(userInfo);
var aps = userInfo.objectForKey("aps");
if (aps !== null) {
var alert = aps.objectForKey("alert");
if (alert !== null && alert.objectForKey) {
userInfoJSON.title = alert.objectForKey("title");
userInfoJSON.body = alert.objectForKey("body");
}
}

firebase._pendingNotifications.push(userInfoJSON);
if (app.applicationState === UIApplicationState.UIApplicationStateActive) {
userInfoJSON.foreground = true;
if (firebase._receivedNotificationCallback !== null) {
firebase._processPendingNotifications();
}
} else {
userInfoJSON.foreground = false;
}
handleRemoteNotification(app, userInfo);
};
}
};
Expand Down Expand Up @@ -161,6 +173,13 @@ firebase.unregisterForPushNotifications = function (callback) {
};

firebase._processPendingNotifications = function() {
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
if (!app) {
application.on("launch", function() {
firebase._processPendingNotifications();
});
return;
}
if (firebase._receivedNotificationCallback !== null) {
for (var p in firebase._pendingNotifications) {
var userInfoJSON = firebase._pendingNotifications[p];
Expand All @@ -174,7 +193,7 @@ firebase._processPendingNotifications = function() {
firebase._receivedNotificationCallback(userInfoJSON);
}
firebase._pendingNotifications = [];
utils.ios.getter(UIApplication, UIApplication.sharedApplication).applicationIconBadgeNumber = 0;
app.applicationIconBadgeNumber = 0;
}
};

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

firebase._registerForRemoteNotifications = function (app) {
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
if (!app) {
application.on("launch", function() {
firebase._registerForRemoteNotifications();
});
return;
}
if (firebase._registerForRemoteNotificationsRanThisSession) {
// ignore
return;
Expand Down
62 changes: 32 additions & 30 deletions scripts/install_ios_entitlements.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,58 @@ var xcode = require('xcode'),
path = require('path'),
pjson = eval('require(\'../../package.json\')'),
iosFolder = path.join('platforms', 'ios'),
data = fs.readdirSync(iosFolder),
data = fs.existsSync(iosFolder) && fs.readdirSync(iosFolder),
projFolder,
projName;

// Find the project folder by looking for *.xcodeproj
if (data && data.length) {
if (!(data && data.length)) {
console.log("platforms/ios does not exist, skip copying entitlements.");
} else {
data.forEach(function (folder) {
if (folder.match(/\.xcodeproj$/)) {
projFolder = path.join(iosFolder, folder);
projName = path.basename(folder, '.xcodeproj');
}
});
}

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

var destFolder = path.join(iosFolder, projName, 'Resources');
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}
var destFolder = path.join(iosFolder, projName, 'Resources');
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}

var destFile = path.join(destFolder, projName + '.entitlements');
var destFile = path.join(destFolder, projName + '.entitlements');

if (!fs.existsSync(destFile)) {
var bundleID = pjson.nativescript.id;
if (!fs.existsSync(destFile)) {
var bundleID = pjson.nativescript.id;

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

var fileData = fs.readFileSync(sourceFile).toString();
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
fs.writeFileSync(destFile, fileData);
var fileData = fs.readFileSync(sourceFile).toString();
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
fs.writeFileSync(destFile, fileData);

var projectPath = path.join(projFolder, 'project.pbxproj'),
pbxProject = xcode.project(projectPath);
var projectPath = path.join(projFolder, 'project.pbxproj'),
pbxProject = xcode.project(projectPath);

pbxProject.parseSync();
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
pbxProject.parseSync();
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));


var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
for (var key in configGroups) {
var config = configGroups[key];
if (config.buildSettings !== undefined) {
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
for (var key in configGroups) {
var config = configGroups[key];
if (config.buildSettings !== undefined) {
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
}
}
}

// write the updated project file
fs.writeFileSync(projectPath, pbxProject.writeSync());
// write the updated project file
fs.writeFileSync(projectPath, pbxProject.writeSync());
}
}
62 changes: 32 additions & 30 deletions scripts/install_ios_entitlements_packed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3706,58 +3706,60 @@ var xcode = __webpack_require__(/*! xcode */ 5),
path = __webpack_require__(/*! path */ 0),
pjson = eval('require(\'../../package.json\')'),
iosFolder = path.join('platforms', 'ios'),
data = fs.readdirSync(iosFolder),
data = fs.existsSync(iosFolder) && fs.readdirSync(iosFolder),
projFolder,
projName;

// Find the project folder by looking for *.xcodeproj
if (data && data.length) {
if (!(data && data.length)) {
console.log("platforms/ios does not exist, skip copying entitlements.");
} else {
data.forEach(function (folder) {
if (folder.match(/\.xcodeproj$/)) {
projFolder = path.join(iosFolder, folder);
projName = path.basename(folder, '.xcodeproj');
}
});
}

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

var destFolder = path.join(iosFolder, projName, 'Resources');
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}
var destFolder = path.join(iosFolder, projName, 'Resources');
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}

var destFile = path.join(destFolder, projName + '.entitlements');
var destFile = path.join(destFolder, projName + '.entitlements');

if (!fs.existsSync(destFile)) {
var bundleID = pjson.nativescript.id;
if (!fs.existsSync(destFile)) {
var bundleID = pjson.nativescript.id;

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

var fileData = fs.readFileSync(sourceFile).toString();
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
fs.writeFileSync(destFile, fileData);
var fileData = fs.readFileSync(sourceFile).toString();
fileData = fileData.replace(/__KEYCHAIN_ACCESS_GROUP__/g, bundleID);
fs.writeFileSync(destFile, fileData);

var projectPath = path.join(projFolder, 'project.pbxproj'),
pbxProject = xcode.project(projectPath);
var projectPath = path.join(projFolder, 'project.pbxproj'),
pbxProject = xcode.project(projectPath);

pbxProject.parseSync();
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));
pbxProject.parseSync();
pbxProject.addResourceFile(path.join(projName, "Resources", projName + ".entitlements"));


var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
for (var key in configGroups) {
var config = configGroups[key];
if (config.buildSettings !== undefined) {
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
var configGroups = pbxProject.hash.project.objects['XCBuildConfiguration'];
for (var key in configGroups) {
var config = configGroups[key];
if (config.buildSettings !== undefined) {
config.buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + projName + '/Resources/' + projName + '.entitlements"';
}
}
}

// write the updated project file
fs.writeFileSync(projectPath, pbxProject.writeSync());
// write the updated project file
fs.writeFileSync(projectPath, pbxProject.writeSync());
}
}


Expand Down