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 134 forks source link

Question about receive SMS #89

Closed Elyft closed 4 years ago

Elyft commented 4 years ago

Hello i'm building an Android SMS App(with default permission system sms app activated), i want to know if i can use your plugin to receive an intent for a new sms coming(even if the app is killed or in background), and please can you provide me a little code to how to achieve the implementation of this intent? thank you

darryncampbell commented 4 years ago

Is this via https://developer.android.com/reference/android/provider/Telephony.Sms.Intents#SMS_RECEIVED_ACTION ? It looks like that Intent is sent via Broadcast. This plugin requires applications to register for a broadcast receiver dynamically so would not work if the application has been killed I'm afraid.

Elyft commented 4 years ago

yes it's sms receive action Can you explain to me a little bit more please?because i don't understand it's an implicit intent right? and to my side i can use a registerBroadcastReceiver to listen to that intent and i need to mark it in the config.xml because Android documentation say that some specific implicit intent like sms receive action need to be mark in manifest.xml. So what can't be able to listen to that intent when my app is killed?

Elyft commented 4 years ago

Hey @darryncampbell this article is made by you right? http://www.darryncampbell.co.uk/2018/07/09/keeping-your-application-running-when-the-device-wants-to-sleep-updated-for-android-p/ Really Good but on my case with Wake lock: Acquired Wifi lock: Acquired Application whitelisted: Yes, My background code stop when my phone screen off and not plugin on power charge Android 8

darryncampbell commented 4 years ago

Hi @Elyft, yes, I am the author of that article but to be honest I would not recommend keeping the device awake so your app can receive an SMS. Yes, the Intent is implicit and yes you can use registerBroadcastReceiver to listen to the intent but once the app is killed Android will remove your broadcast receiver. You can't receive implicit intents via the application manifest on Oreo onwards, https://developer.android.com/about/versions/oreo/background#broadcasts, and I don't think this SMS Intent is on the allowed whitelist.

Elyft commented 4 years ago

ok thank you so have you an idea about how the sms app like qksms or pulse or even google message are doing to receive sms event when the user close their apps? I had to ask to the qksms developper one day My question: Hello @moezbhatti how do you make to keep your app awake and receive sms in background when the phone have the screen turn off on android 8+ without ongoing foreground notification and no FCM His answer: It doesn't need to be awake. The SMS intents have special permission by the android system to make sure that they get delivered regardless of the wake state of the phone. I'm sorry, I can't remember where I learned that information from. But the point is that in my case, I didn't have to do anything special, because I'm listening for Intents that are fired by the system. If I was to set up my own custom intents, then I would need some sort of foreground service

Do you think that it's true @darryncampbell ?

darryncampbell commented 4 years ago

Sorry but I am not personally familiar with how SMS apps work in this case - I presume there must be some accommodation to receiving SMS even when the app is not running and the response from @moezbhatti makes sense so I defer to them in preference to my original responses (which were speaking about Intents generically).

Elyft commented 4 years ago

ok thank you for your help Last question if you allow me can you please give the code of i can with your plugin implement the broadcast receiver for sms?what can i add inside the config.xml

darryncampbell commented 4 years ago

You add the broadcast receiver to the manifest don't you? You need to update the manifest file by editing the config.xml - this SO post looks relevant: https://stackoverflow.com/questions/40446058/cordova-change-androidmanifest-using-config-xml-file

ishigo1987 commented 4 years ago

Hello @darryncampbell i need your help for some problem that i've According to this documentation ( https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html ) if i want to provide to my user a way to make my app the default sms app i need in java to do that thing Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); startActivity(intent);

i want to know how can i do that with your plugin , please can you give me a little help and writing me how can i do that?

darryncampbell commented 4 years ago

@ishigo1987 there are some examples of calling StartActivity via this plugin at https://github.com/darryncampbell/plugin-intent-api-exerciser/blob/master/www/js/index.js. It looks like you need to provide the appropriate action and extra as defined at https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

ishigo1987 commented 4 years ago

In my case i need to use startActivity right?

ishigo1987 commented 4 years ago

ok let me find and i will come back to you thank you

Elyft commented 4 years ago

@darryncampbell hello how are you? I trying the following code and i have a Reference Error Telephony is not defined

window.plugins.intentShim.startActivityForResult(
   {
      action: Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT

   },
   function (intent) {
      console.log(intent)
   },
   function () {
      console.log("StartActivityForResult failure");
   });

according to the documentation provided by this link https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html i need to create a new Intent with this action Where am i wrong?

Elyft commented 4 years ago

i'm the same guy of @ishigo1987

darryncampbell commented 4 years ago

Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT is a String constant - you need to provide the actual string, see https://developer.android.com/reference/android/provider/Telephony.Sms.Intents.html#ACTION_CHANGE_DEFAULT

Constant Value: "android.provider.Telephony.ACTION_CHANGE_DEFAULT"

Elyft commented 4 years ago

Nice Thank You very much

Elyft commented 4 years ago

If you allow me i've another question According your link use ACTION_CHANGE_DEFAULT is no longer use for Android Q Instead we need to use roleManager Your plugin can handle roleManager for Intent?

darryncampbell commented 4 years ago

Your plugin can handle roleManager for Intent?

No, this Plugin does not support Role manager or the call to createRequestRoleIntent and I'm not aware of any that do I'm afraid.

darryncampbell commented 4 years ago

Closing due to inactivity