'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
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
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.
only know the german names... So i write them as i think...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...
Then check the stock app if you can prevent notifications from this app.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 ) and then disallow the Stock app to show Notifications...
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> )
'-----------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();
}
}
}
Where is the link to the post? You just posted a link to stackoverflow....I found a sample on
#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
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.