Android Question SMS reading without NOTIFICATON (hide) on receving time

Tayfur

Well-Known Member
Licensed User
Longtime User
I readed alot topic in forum. And I feel like soup for my brain.

Now; I reading a new sms when i recived with @Erel code.
So, in same time ; Phone showed notification.See picture.


Can I hide/cancel this notification. (I want to read with hide)


notif.png
 

eurojam

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,
you have to intercept the sms message, see this tutorial: https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/
you can also use the phone libraray to intercept sms messages. But nevertheless, it depends on the sms programm on your device, some of them have a higher priority then the intercept from the phone lib...

here an example for a service module howto intercept with the phone lib:
B4X:
'Code by Stefan Giese, with some ideas from the great B4A forum
'2015/10/18
#Region  Service Attributes
    #StartAtBoot: True
  
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Service_Create
Dim SIC As SmsInterceptor
SIC.Initialize2("SIC", 2147483647)
'set the interceptor to the highest priority.
'May be there will be clients which have a higher priority.
'Tested with google hangouts works

End Sub
Sub Service_Start (StartingIntent As Intent)
 
End Sub

Sub Service_Destroy

End Sub

Sub SIC_MessageReceived ( From As String, Body As String) As Boolean
   Dim pSMS As PhoneSms
   Dim fWords As List 'List wih words to check
   Dim returnWord As String
   Dim ReturnText As String
 
   fWords.Initialize
   
   'The return words
   fWords.AddAll(Array As String("Test1","Test2","Test3"))
 
   Dim ReturnSMS As Boolean = False
  'Check for the F-words
   For Each w As String In fWords
      If Body.Contains(w) Then  
          ReturnSMS = True
        returnWord = w
        Exit
      End If      
   Next

    If ReturnSMS Then
      'send message to sender
      ReturnText = "Your message was not delivered because it contains the word " & returnWord
      pSMS.Send(From, ReturnText)
      Log ("message returned")
      Return True 'No processing
    Else
       Log ("message delivered")
       Return False 'Process normal SMS
    End If

End Sub
 
Last edited:
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
I think we can't hide easily, I had been reading around in the forum time ago, I had the same requirement,, and the conclusion is we need to put our SMS interceptor APP as a principal APP to intercept and handle the SMS, I mean, remplace the original of android SMS program. That is my understanding.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,
you have to intercept the sms message, see this tutorial: https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/
you can also use the phone libraray to intercept sms messages. But nevertheless, it depends on the sms programm on your device, some of them have a higher priority then the intercept from the phone lib...

here an example for a service module howto intercept with the phone lib:
B4X:
'Code by Stefan Giese, with some ideas from the great B4A forum
'2015/10/18
#Region  Service Attributes
    #StartAtBoot: True
 
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Service_Create
Dim SIC As SmsInterceptor
SIC.Initialize2("SIC", 2147483647)
'set the interceptor to the highest priority.
'May be there will be clients which have a higher priority.
'Tested with google hangouts works

End Sub
Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub SIC_MessageReceived ( From As String, Body As String) As Boolean
   Dim pSMS As PhoneSms
   Dim fWords As List 'List wih words to check
   Dim returnWord As String
   Dim ReturnText As String

   fWords.Initialize
  
   'The return words
   fWords.AddAll(Array As String("Test1","Test2","Test3"))

   Dim ReturnSMS As Boolean = False
  'Check for the F-words
   For Each w As String In fWords
      If Body.Contains(w) Then 
          ReturnSMS = True
        returnWord = w
        Exit
      End If     
   Next

    If ReturnSMS Then
      'send message to sender
      ReturnText = "Your message was not delivered because it contains the word " & returnWord
      pSMS.Send(From, ReturnText)
      Log ("message returned")
      Return True 'No processing
    Else
       Log ("message delivered")
       Return False 'Process normal SMS
    End If

End Sub

thank you for your share;

İt is nice fast reply sms answers. I will use your code.

But; I see notification eachtime :(
How can i do it? (you said tome change "the highest priority." How can i change)
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
I think we can't hide easily, I had been reading around in the forum time ago, I had the same requirement,, and the conclusion is we need to put our SMS interceptor APP as a principal APP to intercept and handle the SMS, I mean, remplace the original of android SMS program. That is my understanding.

How can i set my app. (from "OS sms app" prioty level )
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How can i set my app. (from "OS sms app" prioty level )
only know the german names... So i write them as i think...

- Settings
- Applications
- Standard-Apps

HERE you need to set your app as the DEFAULT Messaging App on your device.
If your app is the default-app then you are allowed to discard new messages. And it is up to your app too to show a Notification or even not...
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
only know the german names... So i write them as i think...

- Settings
- Applications
- Standard-Apps

HERE you need to set your app as the DEFAULT Messaging App on your device.
If your app is the default-app then you are allowed to discard new messages. And it is up to your app too to show a Notification or even not...

I use CM12.
Messages>>Setting>> Default SMS app>> (Phone showed only stock sms app)
I want to only cancel "revice sms notification"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I want to only cancel "revice sms notification"
Then check the stock app if you can prevent notifications from this app.
Otherwise you can try to do a LOOOONG CLICK on the Notification to get the Systems Notificationblocker (maybe i am using a app which does this. dont know exactly :D) and then disallow the Stock app to show Notifications...
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
Then check the stock app if you can prevent notifications from this app.
Otherwise you can try to do a LOOOONG CLICK on the Notification to get the Systems Notificationblocker (maybe i am using a app which does this. dont know exactly :D) and then disallow the Stock app to show Notifications...

I used @Erel' code.
Now; I see my app in SMS setting.
After I will change default app with my app.
And send to me SMS to us.And. My app's service1 RUN.

BUT. Phone cant use default SMS APP.(reading/writing etc...)
I want works my app only with special sms. except that Phone work a stock APP

Thank you for your support. I think this way is wrong way.


Turns out you need to manage all the options or it will not be listed. You need to have three services and add this code:
B4X:
SetReceiverAttribute(service1, android:permission, "android.permission.BROADCAST_SMS")
AddReceiverText(service1, <intent-filter>
  <action android:name="android.provider.Telephony.SMS_DELIVER" />
  </intent-filter>
)
SetReceiverAttribute(service2, android:permission, "android.permission.BROADCAST_WAP_PUSH")
AddReceiverText(service2,  <intent-filter>
  <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
  <data android:mimeType="application/vnd.wap.mms-message" />
  </intent-filter>
)
SetServiceAttribute(service3, android:permission, "android.permission.SEND_RESPOND_VIA_MESSAGE")
SetServiceAttribute(service3, android:exported, "true")
AddServiceText(service3,  <intent-filter>
  <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="sms" />
  <data android:scheme="smsto" />
  <data android:scheme="mms" />
  <data android:scheme="mmsto" />
  </intent-filter>
)
AddActivityText(main,
<intent-filter>
  <action android:name="android.intent.action.SEND" /> 
  <action android:name="android.intent.action.SENDTO" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="sms" />
  <data android:scheme="smsto" />
  <data android:scheme="mms" />
  <data android:scheme="mmsto" />
  </intent-filter>
)
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
I found a sample on http://stackoverflow.com/

is it works? (I cant convert B4A :( )

B4X:
'-----------MAINFAST------------

<receiver android:name=".SMSReceiver" >
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
'-----------------------------------
public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
     if(conditionMatches){
     abortBroadcast();
     }
    }
}
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I found a sample on
Where is the link to the post? You just posted a link to stackoverflow....
I guess the solution is NOT for newer android versions!?
Otherwise you can add the receiver to your manifest and use the broadcastreceiver lib.
I guess you are only allowed to abort the broadcast if YOUR app is the default messaging app as described in the Post #1-10
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Don't waste your time searching for workaround. Starting from KitKat your app MUST be declared as SMS default app, then when you receive the SMS you can do what you want.
You will find tons of posts here that talk about this (search for "SMS Kitkat")
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
Thank for helps @DonManfred @eurojam @marcick ...

3th party app cant change sms notifications.

I try this code. (notification listener lib).
I updated some lines.
this code claer notification when if notifications has same my conditions.

most of time; My app catch and delete sms notification before stock sms APP.

In this case, enough for me.


B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private listener As NotificationListener
End Sub
Sub Service_Create
    Log("1")
    listener.Initialize("listener")

End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("2")
    Log(DateTime.Now &" - 1 -"&StartingIntent.Action)
    If listener.HandleIntent(StartingIntent) Then Return
    Log("3")
End Sub

Sub Listener_NotificationPosted (SBN As StatusBarNotification)
    Log("4")
    Log("NotificationPosted, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
    Dim p As Phone
    If p.SdkVersion >= 19 Then
        Log("5")
        Dim jno As JavaObject = SBN.Notification
        Dim extras As JavaObject = jno.GetField("extras")
        extras.RunMethod("size", Null)
        Log(extras)
        Dim title As String = extras.RunMethod("getString", Array As Object("android.title"))
        Dim text As String = extras.RunMethod("getString", Array As Object("android.text"))
        'Dim text As String = extras.RunMethod("getBoolean", Array As Object("android.showWhen"))
        'Dim text As String = extras.RunMethod("getInt", Array As Object("android.icon"))
        'Dim text As Object = extras.RunMethod("getObject", Array As Object("android.bigText"))
        LogColor("Text = " & text, Colors.Blue)
        LogColor("Title = " & title, Colors.Blue)
        If title.Trim="TAYFUR" Then ''----------->>> this is my conditions
            LogColor("Clear", Colors.Red)
            listener.ClearNotification(SBN)
            '************DELETE ACTIVE NOTIFICATION**************************************
            'LogColor(DateTime.Time(DateTime.Now)& " - Cleared:", Colors.Red)
            Log(text)
        End If
    End If
    Log("6")
End Sub

Sub Listener_NotificationRemoved (SBN As StatusBarNotification)
    Log("7")
    Log("NotificationRemoved, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
End Sub

Sub ClearAll
    Log("8")
    listener.ClearAll
End Sub

Sub GetActive
    Log("9")
    listener.GetActiveNotifications
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
Thank for helps @DonManfred @eurojam @marcick ...

3th party app cant change sms notifications.

I try this code. (notification listener lib).
I updated some lines.
this code claer notification when if notifications has same my conditions.

most of time; My app catch and delete sms notification before stock sms APP.

In this case, enough for me.


B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private listener As NotificationListener
End Sub
Sub Service_Create
    Log("1")
    listener.Initialize("listener")

End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("2")
    Log(DateTime.Now &" - 1 -"&StartingIntent.Action)
    If listener.HandleIntent(StartingIntent) Then Return
    Log("3")
End Sub

Sub Listener_NotificationPosted (SBN As StatusBarNotification)
    Log("4")
    Log("NotificationPosted, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
    Dim p As Phone
    If p.SdkVersion >= 19 Then
        Log("5")
        Dim jno As JavaObject = SBN.Notification
        Dim extras As JavaObject = jno.GetField("extras")
        extras.RunMethod("size", Null)
        Log(extras)
        Dim title As String = extras.RunMethod("getString", Array As Object("android.title"))
        Dim text As String = extras.RunMethod("getString", Array As Object("android.text"))
        'Dim text As String = extras.RunMethod("getBoolean", Array As Object("android.showWhen"))
        'Dim text As String = extras.RunMethod("getInt", Array As Object("android.icon"))
        'Dim text As Object = extras.RunMethod("getObject", Array As Object("android.bigText"))
        LogColor("Text = " & text, Colors.Blue)
        LogColor("Title = " & title, Colors.Blue)
        If title.Trim="TAYFUR" Then ''----------->>> this is my conditions
            LogColor("Clear", Colors.Red)
            listener.ClearNotification(SBN)
            '************DELETE ACTIVE NOTIFICATION**************************************
            'LogColor(DateTime.Time(DateTime.Now)& " - Cleared:", Colors.Red)
            Log(text)
        End If
    End If
    Log("6")
End Sub

Sub Listener_NotificationRemoved (SBN As StatusBarNotification)
    Log("7")
    Log("NotificationRemoved, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
End Sub

Sub ClearAll
    Log("8")
    listener.ClearAll
End Sub

Sub GetActive
    Log("9")
    listener.GetActiveNotifications
End Sub

Sub Service_Destroy

End Sub

Hi Tayfur
were you testing this code, in which android versions? I have the same requirements.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur
were you testing this code, in which android versions? I have the same requirements.

Hi @Daniel-White ;
I used Samsung S2 (cyangenmod CM12 - 5.0.1 lolipop)for test.

usually, This code catch notifications. Sometimes, its late works. and I listen sounds.

Note:
https://www.b4x.com/android/forum/t...er-library-notificationlistenerservice.35630/

I changed it.
First step: Change notification access , you can add your app.
After test your code.
TITLE= senders phone records name.
You can it for code.
 
Upvote 0
Top