Ionic/Cordova: Add intent-filter using config.xml
I had the same problem, but the idea of installing (and then remembering or documenting the dependency on) a bunch of npm dependencies and then using a big general-purpose hook was far too heavyweight for what I needed.
Hooks can be simple shell scripts which is often a much more straightforward way of modifying text files. In my case I only needed to add an intent-filter to the MainActivity
activity which is a trivial job for sed
; I just created the file hooks/after_prepare/020_add_moozvine_intents.sh
with the content:
#!/usr/bin/env zsh
MANIFEST=${0:h}/../../platforms/android/AndroidManifest.xml
[[ -e $MANIFEST ]] || { print "Manifest not found at $MANIFEST." ; exit 1; }
grep -q HANDLE_MOOZVINE_NOTIFICATION $MANIFEST && { print "Manifest already modified. Nothing to do."; exit 0; }
AFTER_LINE='android:name="MainActivity"'
ADDITION='\
<intent-filter>\
<action android:name="HANDLE_MOOZVINE_NOTIFICATION" />\
<category android:name="android.intent.category.DEFAULT" />\
</intent-filter>
';
sed -i -e "/${AFTER_LINE}/a${ADDITION}" $MANIFEST
Job done. You can use a similar approach for any simple textual modifications to generated files.
Resolved!
I can't do it using Ionic or Cordova: it's a PhoneGap feature (see this Stackoverflow answer)
I can do it in two other ways:
- Using a custom Cordova plugin
- Using a hook
I prefered the second way. I found an interesting hook for my purposes. Note: Rembember to install some packages:
npm install lodash elementtree plist --save-dev
Sadly this hook merges tags. So I wrote a little changed version of this hook: see here. You can put this hook in /hooks/after_platform_add.
Now I have my intent-filter configuration in config.xml:
<platform name="android">
<config-file target="AndroidManifest.xml" parent="application/activity">
<intent-filter><!-- ... --></intent-filter>
</config-file>
<!-- ... -->
</platform>
And I can update AndroidManifest.xml regenerating android platform:
ionic platform remove android && ionic platform add android
Cordova 9 now directly supports using <config-file>
and <edit-config>
sections, like in plugin.xml files.
Without using any plugin or hook, you can directly do the following, for example, to add an intent filter to AndroidManifest.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="yourdomain.app" version="1.7.8"
xmlns="http://www.w3.org/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<!-- ... -->
<platform name="android">
<!-- ... -->
<config-file parent="application" target="AndroidManifest.xml">
<activity android:label="webIntentFilter" android:name="yourdomain.app">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="yourdomain.com" android:scheme="https" />
</intent-filter>
</activity>
</config-file>
</platform>
<!-- ... -->
</widget>
Don't forget to add attribute xmlns:android="http://schemas.android.com/apk/res/android"
into your <widget>
tag to avoid unbound prefix
error at build.