Sending url to ionic android app via webintents from another app

前端 未结 3 1450
时光说笑
时光说笑 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 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 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(() => {
    
        (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(() => {
          (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.

提交回复
热议问题