Cordova receive shared data from other app

2019-05-25 00:24发布

问题:

At first please do not mar this question as duplicate. All other questions are old and I've tried solutions from most of them and none of them work.

I'm working on an Android app and I'd added my app to share Android menu. I'd like to add functionality so if user clicks my app on the share list e.g. in Chrome browser or Google Drive app, my app will receive the data from that app e.g. from Chrome it wold be URL.

I've tried to use different plugins and read many posts about how to use intent in my app. Unfortunately none of them work. I've tried:

  • Sending url to ionic android app via webintents from another app - this one looked very promising
  • https://www.npmjs.com/package/phonegap-webintent
  • https://github.com/napolitano/cordova-plugin-intent - this one looked promising as well
  • https://github.com/okwei2000/webintent
  • https://github.com/stample/cordova-sharingreceptor

Did anyone got this working? My AndroidManifest.xml is as follows - the part about intent:

        <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" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>

In my config.xml I have:

<preference name="AndroidLaunchMode" value="singleTask" />

And I'm not getting data I'm expecting.

When I'm using https://github.com/napolitano/cordova-plugin-intent plugin I'm getting the intent but clipItems element is missing and the plugin is useless.

What am I doing wrong?

Any help appreciated.

回答1:

Finally I've done it. I used https://github.com/napolitano/cordova-plugin-intent ver 0.1.3 and

window.plugins.intent.setNewIntentHandler(function (intent) {
    // To get browser URL I had to use
    var subject = intent.extras['android.intent.extra.SUBJECT'];
    var url = intent.extras['android.intent.extra.TEXT'];
    // For Chrome, Opera and FireFox it works. For build in Android browser TEXT holds page title and URL joined with +
});

Although ver 0.1.3 has

window.plugins.intent.getRealPathFromContentUrl(contentUrl, function (realPath) {}, function () {});

available I couldn't use it because this didn't give me the data I wanted. contentUrl is different for differen browsers e.g. for Chrome it is uri and for build in Android browser it's text.

I installed ver 0.1.3 using CLI

phonegap plugin add https://github.com/napolitano/cordova-plugin-intent.git#61a47ae8c28a62004eeff4ed96a6f3c64c271a16

I couldn't add it using tag in config.xml

I hope this will help others.