locked
Filter Received SMS (Delete SMS if it contains certain text) RRS feed

  • Question

  • Hi all ....

    I'm using this code to intercept SMS , But I need to know :

    1 - How to delete received SMS that contain a certain text ???

    This is the code I use to intercept SMS

    Private Sub MonitorNotifications()
            Try
               
                _smsInterceptor = New MessageInterception.MessageInterceptor()
                AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
            Catch ex As Exception
                MsgBox("MonitorNotifications :- " & ex.Message)
            End Try
        End Sub
    
    Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
            Try
                ' Cast to SmsMessage to access message body Not expecting to receive any 
                ' non-SMS messages but use "as" to cast to be extra safe
                Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
                If Not newMessage Is Nothing Then
    
                    Select Case newMessage.Body
                        Case "XXXX"
                            'How can I delete this SMS 
                    End Select
                End If
            Catch ex As Exception
                MsgBox("SMSInterceptor_MessageReceived :-" & ex.Message)
            End Try
        End Sub
    Many thanks in advance.
    Saturday, March 20, 2010 9:38 AM

Answers

  • And finally I'v found the code

    _smsInterceptor = New MessageInterceptor(InterceptionAction.NotifyAndDelete)
                _smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, "XXXX ", False)
                AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
    
    Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
            Try
                Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
                If Not newMessage Is Nothing Then
                    Dim SMSBody As String = newMessage.Body.ToLower
    
                    If SMSBody.Contains("hello") Then
                        'do something
                    ElseIf SMSBody.Contains("world") Then
                          'do something
                    End If
    
                End If
            Catch ex As Exception
                MsgBox("SMSInterceptor_MessageReceived :-" & ex.Message)
            End Try
        End Sub

    I hope this code may help someone else.

    Many thank for all help. 

    Monday, March 22, 2010 1:09 PM

All replies

  • Hi,

    You could setup a new Message Interceptor that includes a condition :-

    http://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.pocketoutlook.messageinterception.messageinterceptor.messagecondition.aspx

    Then set the InterceptionAction to be NotifyAndDelete :-

    http://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.pocketoutlook.messageinterception.messageinterceptor.interceptionaction.aspx

    Your application will then handle the incoming SMS and then discard it if the particular condition has been met. This condition is the same condition that you are applying in your switch statement.

    Hope this helps.

    Paul Diston


    http://www.smartmobiledevice.co.uk/
    Saturday, March 20, 2010 10:13 AM
  • Many thanks Paul , But I think I'm gonna need more help , I need an example that handle many different SMS text , And if the SMS text doesn't match these conditions it moves to Inbox .

     

    ex :

    If I received SMS with body text "Hello" then do something , And don't delete it

    If I received SMS with body text "World" then do something , And don't delete it

    If I received SMS with body text "XXX" then do something , And Delete it

     

    Many thanks in advance.

    Saturday, March 20, 2010 4:25 PM
  • And finally I'v found the code

    _smsInterceptor = New MessageInterceptor(InterceptionAction.NotifyAndDelete)
                _smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, "XXXX ", False)
                AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
    
    Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
            Try
                Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
                If Not newMessage Is Nothing Then
                    Dim SMSBody As String = newMessage.Body.ToLower
    
                    If SMSBody.Contains("hello") Then
                        'do something
                    ElseIf SMSBody.Contains("world") Then
                          'do something
                    End If
    
                End If
            Catch ex As Exception
                MsgBox("SMSInterceptor_MessageReceived :-" & ex.Message)
            End Try
        End Sub

    I hope this code may help someone else.

    Many thank for all help. 

    Monday, March 22, 2010 1:09 PM