Sending url to ionic android app via webintents from another app

前端 未结 3 1444
时光说笑
时光说笑 2020-12-15 11:24

Looking for an updated solution, running the latest ionic 1.1.0 release which uses Cordova 5.x. Trying to be able to browse a website in chrome and send that url to my ionic

相关标签:
3条回答
  • 2020-12-15 11:45

    Found out that the problem was due to how I set up my AndroidManifest.xml file.

    I was using an extra activity tag, when I should have been including the intent in the 1 activity.

    For instance I had:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="ShareActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
    

    When I should have had:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
         <intent-filter android:label="@string/launcher_name">
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
         </intent-filter>
    </activity>
    
    0 讨论(0)
  • 2020-12-15 11:46

    This question is a bit aged. But i had a more or less similar challenge. I had no luck with the WebIntent plugin and therefore i developed my own plugin for dealing with Intents on Android.

    You may check this: https://github.com/napolitano/cordova-plugin-intent

    While i'm still working on this plugin for my own projects, it's yet almost usable and documentation should be good enough to get a foot into the door.

    Some background:

    To allow third party apps to send content to your app, you must add an intent-filter to your MainActivity in the AndroidManifest.xml.

    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <action android:name="android.intent.action.SEND_MULTIPLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="*/*" />
    </intent-filter>
    

    While you can do this manually, this has some disadvantages - so you probably want to have a hook or something similar. If you go to the repository above, you'll find and example for this.

    Additionally to the intent-filter, you currently need an plugin that allows you a) to access the cordova intent (that's important to get access to sent content on application startup) and to receive notifications if the onNewIntent event was triggered, which happens, if content is sent while your application is running.

    That's exaclty what my plugin does. It gives you access to the cordova intent and allows you to handle the onNewIntent event.

    Examples:

    window.plugins.intent.getCordovaIntent(function (Intent) {
        console.log(Intent);
    }, function () {
        console.log('Error');
    });
    
    window.plugins.intent.setNewIntentHandler(function (Intent) {
        console.log(Intent);
    });
    

    You will receive a limited intent object as result on success. This can be processed by your app.

    Example:

    {
        "action": "android.intent.action.SEND_MULTIPLE",
        "clipItems": [
            {
                "uri": "file:///storage/emulated/0/Download/example-document.pdf"
            },
            {
                "uri": "file:///storage/emulated/0/Download/example-archive.zip"
            }
        ],
        "flags": 390070273,
        "type": "*/*",
        "component": "ComponentInfo{com.example.droid/com.example.droid.MainActivity}",
        "extras": "Bundle[mParcelledData.dataSize=596]"
    }
    

    While this example actually shows JSON, you will receive a ready-to-use object.

    Please note that i develop the plugin currently only for my own needs. However it may work also for you. Currently no angular examples were added to the README, but i think that should not be a big problem.

    0 讨论(0)
  • 2020-12-15 12:01

    I want to add to @axel-napolitano's answer to make it explicit as to where the code should be added in an Ionic 3 application. Disclaimer: I can barely code Ionic apps.

    At the time of writing, I am currently using Ionic 3.9. After a lot of trial and error and piecing together answers on SO, I was able to get this working (i.e. to have another app share to my Ionic app.)

    At this point, you should have already compiled your app for Android and hooked up your Android phone to your computer so that the app runs on it. Look for documentation on how to run ionic apps on a physical Android device.

    Here's the command: ionic cordova run android -l -c.

    You should also have added the <intent-filter> into AndroidManifest.xml.

    Put the code in /pages/home/home.ts - ensure your constructor has the 'platform' injected.

    export class HomePage {
    
      constructor(private platform:Platform){
         ...
       }
    ...
    }
    

    Create this method (in home.ts) to receive the intent for when the app is not in a loaded state:

    ionViewDidLoad(){
      this.platform.ready().then(() => {
    
        (<any>window).plugins.intent.getCordovaIntent(
          function (Intent) {
            //you should filter on the intents you actually want to receive based on Intent.action
            console.log('intent received on app launch' + JSON.stringify(Intent));
        },
        function () {
            console.log('Error getting cordova intent');
        }
        );
      });
    }
    

    To receive the intent for when the app is already loaded:

    ionViewDidEnter() {
        this.platform.ready().then(() => {
          (<any>window).plugins.intent.setNewIntentHandler(
            function (Intent) {
                console.log('new intent' + JSON.stringify(Intent) );
            }
        );
    
        });
      }
    

    Fun fact: the "official" web intent plugin (darryncambell's) - which I couldn't get to work - credits Napolitano's plugin. Source

    I eventually got darryncambell's plugin to work using the same methodology.

    0 讨论(0)
提交回复
热议问题