Search code examples
cordovacordova-pluginsvisual-studio-cordovataco

Unable to add push notifications entitlement to iOS production with cordova-plugin-fcm plugin


I am trying to install cordova-plugin-fcm plugin in a blank Cordova app using Visual Studio 2017. I install the plugin and add the google-plist files etc. On my mac i have xcode 7.2.1.

When i build an ipa from visual studio it generates an IPA, but it does not have push notifications enabled. I have added all the source code, including plugins folders, to bitbucket. Link below; https://bitbucket.org/quintonn/pushnotificationtest

What am i missing to get push notifications enabled on my iphone? FYI, i can get it workig on android quite easily.

I don't want to have to open xcode to enable push notifications. I know it's possible directly from Visual Studio using a hook or xcconfig or config.xml. But i just can't get it working and i've spent about 3 weeks now unable to get this working. And i'm just feeling in the dark as all the reading material online is for different versions of cordova, or visual studio, or xcode, or phonegap or ionic or whatever.


Solution

  • I've spent many, many, many.... hours on this problem.

    And i've come up with a cordova after_prepare hook that solves my problem on XCode 7 and 8. Here it is...

    "use strict";
    
    var fs = require('fs');
    var path = require('path');
    var xcode = require('xcode');
    
    module.exports = function (context)
    {
        var encoding = 'utf-8';
        var plist = fs.readFileSync(path.resolve(__dirname, "../GoogleService-Info.plist"), encoding);
    
    
        fs.writeFileSync(path.resolve(__dirname, "../platforms/ios/GoogleService-Info.plist"), plist, encoding);
    
        var projectPath = path.resolve(__dirname, "../platforms/ios/APP NAME.xcodeproj/project.pbxproj");
        var pbxFile = fs.readFileSync(projectPath, encoding);
    
        var proj = new xcode.project(projectPath);
    
        proj = proj.parseSync();
    
        var pbxGroupKey = proj.findPBXGroupKey({
            name: "Resources"
        });
        proj.removeResourceFile('GoogleService-Info.plist', {}, pbxGroupKey);
        proj.addResourceFile('GoogleService-Info.plist', {}, pbxGroupKey);
    
        proj.addBuildProperty('"CODE_SIGN_IDENTITY[sdk=iphoneos*]"', '"iPhone Distribution"', 'Release');
        proj.addBuildProperty('DEVELOPMENT_TEAM', 'XXXXXXXX', 'Release');
    
        proj.addBuildProperty('PROVISIONING_PROFILE', "XXXXXXXX-XXXXXXXX-XXXX-XXXX-XXXXXXXX", 'Release');
        proj.addBuildProperty('PROVISIONING_PROFILE_SPECIFIER', '"NAME OF PROFILE"', 'Release');
        proj.addBuildProperty('TARGETED_DEVICE_FAMILY', '"1,2"', 'Release');
    
        proj.addTargetAttribute("DevelopmentTeam", "XXXXXXXX");
        var pushEntitlement = "{com.apple.Push ={enabled = 1;};}";
        proj.addTargetAttribute("SystemCapabilities", pushEntitlement);
    
        /*var attributes = proj.getFirstProject()['firstProject']['attributes'];
        if (attributes['TargetAttributes'] === undefined)
        {
            attributes['TargetAttributes'] = {};
        }
        var target = proj.getFirstTarget();
        if (attributes['TargetAttributes'][target.uuid] === undefined)
        {
            attributes['TargetAttributes'][target.uuid] = {};
        }
        attributes['TargetAttributes'][target.uuid]["SystemCapabilities"] = "{com.apple.Push ={enabled = 1;};}";
        */
        fs.writeFileSync(projectPath, proj.writeSync());
    
        fs.writeFileSync(path.resolve(__dirname, "../platforms/ios/APP NAME/Resources/GoogleService-Info.plist"), plist, encoding);
        fs.writeFileSync(path.resolve(__dirname, "../platforms/ios/APP NAME/Resources/Resources/GoogleService-Info.plist"), plist, encoding);
    };
    

    Just remember to update your APP Name, provisioning profile name and team id.