Search code examples
ionic-frameworkxcode8ios10xcodebuildfastlane

Ionic/cordova: how to add push capability with fastlane or xcodebuild?


I have an Ionic project which uses Ionic.io push messaging. It is built with Fastlane and deployed through HockeyApp.

Since upgrading to Xcode 8 push notifications no longer work on iOS 10.

I have an entitlements file containing the push entitlement and it is added to the xcode project file with a ruby script, see https://github.com/fastlane/fastlane/issues/6544

When I build the project with Fastlane push still doesn't work. When I open the project file in Xcode and look at the capabilities section it shows a checkmark in "Add the push notification entitlement to your entitlements file" but shows an error in "add the push notification feature to your app id".

If I press "fix" and rebuild, push does work.

So my problem is:

I want to be able to get the push capability enabled correctly, only using Fastlane, xcodebuild, ruby or whatever, as long as it's only in the command line and allows my ionic project to be built cleanly.


Solution

  • So I managed to get it working by doing two things:

    • I deleted all old provisioning profiles from xcode. This reversed the problem, so Xcode complained that the push entitlement was not added to my entitlements file, but there was a checkmark in "add the push notification feature to your app id".
    • Next I changed the script to include the project file in the project itself. I have been informed by https://github.com/Azenet that probably, this is related to not using match from Fastlane.

    Thanks to https://github.com/Azenet and https://github.com/hjanuschka for getting me 90% of the way.

    #!/usr/bin/env ruby
    require 'xcodeproj'
    
    name = ARGV[0]
    projectpath = "../platforms/ios/" + name + ".xcodeproj"
    puts "Adding entitlement push to " + name
    puts "Opening " + projectpath
    proj = Xcodeproj::Project.open(projectpath)
    entitlement_path = name + "/" + name + ".entitlements"
    
    group_name= proj.root_object.main_group.name
    
    file = proj.new_file(entitlement_path)
    
    attributes = {}
    proj.targets.each do |target|
        attributes[target.uuid] = {"SystemCapabilities" => {"com.apple.Push" => {"enabled" => 1}}}
        target.add_file_references([file])
        puts "Added to target: " + target.uuid
    end
    proj.root_object.attributes['TargetAttributes'] = attributes
    
    proj.build_configurations.each do |config|
        config.build_settings.store("CODE_SIGN_ENTITLEMENTS", entitlement_path)
    end
    puts "Added entitlements file path: " + entitlement_path
    
    proj.save