darryncampbell / darryncampbell-cordova-plugin-intent

General purpose intent shim layer for cordova appliations on Android. Handles various techniques for sending and receiving intents.
MIT License
86 stars 136 forks source link

App is not receiving Broadcast intent #44

Closed DieOptimistin closed 6 years ago

DieOptimistin commented 6 years ago

Another apps shares an url and my app should offer himself to handle it. Nevertheless my app is not appearing on the share list. Also, there is an runtime error if I remove the broadcast callback, if I add the callback the compiler throws an error...

Could you show a working sample for the latest Ionic version?

ERROR TypeError: Wrong type for parameter "successCallback" of IntentShim.getIntent: Expected Function, but got Undefined. at Object.checkArgs (cordova.js:428) at IntentShim.getIntent (plugins/com-darryncampbell-cordova-plugin-intent/www/IntentShim.js:74)

`ionViewDidLoad() { console.log ("ionViewDidLoad");

  this.intent.registerBroadcastReceiver({
    filterActions: [
        this.intent.ACTION_SEND
        ], 
    filterCategories: [
          'com.android.intent.category.DEFAULT'
          ]
  }/*, 
    function(intent) {
        //  Broadcast received
        console.log('Received Intent: ' + JSON.stringify(intent.extras));
    }*/
  );

  let result = this.intent.getIntent();
  console.log(result);
} 
darryncampbell commented 6 years ago

In order to be in the share list the app needs to declare an intent-filter in the manifest. This plugin does not support that but you should be able to modify the manifest for your ionic app: http://cordova.apache.org/docs/en/7.x/plugin_ref/spec.html#edit-config

The problem with registerBroadcastReceiver is a known issue. I have been told by several people that it would be fixed but I think I need to raise the pull request myself against https://github.com/ionic-team/ionic-native/blob/master/src/@ionic-native/plugins/web-intent/index.ts . I'll label this issue as todo for that reason. The workaround is to use the plugin directly:

(<any>window).plugins.intentShim.registerBroadcastReceiver({
      filterActions: [      ],
      filterCategories: []
    },
      function (intent) {});

An example of this in use would be https://github.com/Zebra/ZebraIonicDemo/blob/master/src/providers/barcode/barcode.ts#L20

DieOptimistin commented 6 years ago

ok, got it working now and facing the next issue.

Manifest: `

            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/*" />
        </intent-filter>`

I followed your proposal, but the BroadcastReceiver is never triggered. My next try:

`ionViewDidEnter() { console.log ("ionViewDidLoad");

  (<any>window).plugins.intentShim.registerBroadcastReceiver({
    filterActions: [
      'android.intent.action.SEND',          
    ],
    filterCategories: [
      'android.intent.category.DEFAULT'
    ]
  },
    function (intent) {
      //  Broadcast received
      console.log('Received Intent: ' + JSON.stringify(intent.extras));
    });
} `
` this.onResumeSubscription = platform.resume.subscribe(() => {
          console.log ("onresume");
          this.intent.getIntent().then((res) => {
              console.log("ionViewDidLoad MYINTENT:", res);
            });
          }
        });
`

by using on resume subscription I receive sometimes an event. When the app was started from the share intent the intent action is fine. When the app was resumed and brought to the front with the share event, the intent action is "android.intent.action.MAIN" and contains no data.

darryncampbell commented 6 years ago

If you share something with an application, the receiving application is started via StartActivity, just checking you are not expecting to receive anything in the broadcast receiver as a result of receiving the action.send intent. Also, please note that the plugin uses a dynamic broadcast receiver which is registered in onResume() and unregistered in onPause(), so you would not receive the broadcast intent in the background. This was done because of all the changes being made to background restrictions in 'O', I didn't want to give the impression the broadcast receiver would continue to work after the application is paused.

I'm not certain why the result of getIntent() is different, under the covers it is just calling https://developer.android.com/reference/android/app/Activity.html#getIntent(). I guess on one scenario it is being launched from a stopped state and in the other scenario it is just being resumed

darryncampbell commented 6 years ago

Raised pull request for registerBroadcastReceiver on the ionic-native project: https://github.com/ionic-team/ionic-native/pull/2491