Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin don´t work on cold start #17

Open
aperezcoinpre opened this issue Dec 12, 2016 · 6 comments
Open

Plugin don´t work on cold start #17

aperezcoinpre opened this issue Dec 12, 2016 · 6 comments

Comments

@aperezcoinpre
Copy link

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?

@rexx-org
Copy link

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 :-(

@FdezRomero
Copy link

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

@rexx-org
Copy link

rexx-org commented Jan 15, 2017

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
Copy link

pwbs commented Aug 1, 2017

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.

@FdezRomero
Copy link

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.

@madewulf
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants