napolitano/cordova-plugin-intent

Plugin don´t work on cold start

Opened this issue · 6 comments

Hi, when the app it´s completly closed (cold start) i try to share an image or a text using this plugin and it don´t work.
When the app it´s open on background the plugin work perfect.

Somebody have this problem?

Yes I have the same problem. Tried SingleTop and SingleTask doesn't work. Works fine if app already running. If the plugin doesn't work on a cold start kind of defeats the purpose of the plugin :-(

The plugin does work, I'm currently using it in a production app without problems. Check your AndroidManifest.xml, maybe you have something missing.

Thanks for the confirmation. I persisted in making it work and have managed to. Below is how I had to make the plugin work. I don't know if this is how it is intended to work, but it did for me; YMMV. My app imports data from a CSV file.
To enable my app to show up in Android's "Open with" menus from various other apps I had to add a new action: android.intent.action.VIEW in the manifest file.
My additions to AndroidManifest.xml are:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" 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.EDIT" />
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:host="*" android:mimeType="text/csv" android:pathPattern=".*\\.csv" />
        </intent-filter>
    </activity>

Then the code to handle the intent:
--- www/index.html

<script type="text/javascript">
  document.addEventListener('deviceready',deviceReady,false)
  function deviceReady()
  {
     enableOpenWith()
  }
  </script>

--- www/js/index.js

function enableOpenWith()
{
   // using previously determined OS
   if ( g_os == 'Android' )
   {
      window.plugins.intent.getCordovaIntent( function( Intent )
      {
         // when starting the app from a cold start (ie NOT from another app passing data),
         // the "current" intent action is MAIN.  Ignore it.
         if ( Intent.action !== 'android.intent.action.MAIN' )
         {
            // the app has started from a VIEW action intent, ie another app is opening ours
            // with a CSV file, so import the data
            handleExternalImport( Intent.data )
         }
      })
      window.plugins.intent.setNewIntentHandler( function( Intent )
      {
         // when our app is brought to the foreground (ie it is currently running) by
         // another app passing a CSV file, this code is called.
         // android.intent.action.MAIN IS called when app brought to foreground so need to limit
         // the import when NOT MAIN intent (Updated this Jan 17 2017)
         if ( Intent.action !== 'android.intent.action.MAIN' )
         {
            handleExternalImport( Intent.data )
          }
      })
   }
   else
   {
      // for iOS use the handleOpenURL. Called from a cold start or when brought to the foreground
      window.handleOpenURL = function (url)
      {
         handleExternalImport( url )
      }
   }
}

Hope this helps others.

pwbs commented

Hi,

there's something I don't get at all and I'm hoping I can get some help! On iOS, when does window.handleOpenURL ever get called?! Adding the XML blahblah (... <key>UIFileSharingEnabled</key><true/> ...) doesn't seem to change anything.

From what I understand, for the app to appear in the sharing menu, we have to implement a "Share Extension" Target. But then, that triggers a quite standard popup in the current app, so my app doesn't come foreground for the sharing.

Thanks in advance for any answer that could help me even a tiny bit.

My understanding is that window.handleOpenURL gets called when iOS asks which app would you like to open a file with. But if you want to get the "intent" by sharing, you will need to create a Share Extension target in your app from Xcode. It's up to you to show native UI or just open your app from there and mark the task as done. In any case, it's not covered by this plugin.

If there are still people interested, I just made a PR who supports opening intents at the start of the activity without having to add js code to handle that special case. #28