lavanya
lavanya

Reputation: 11

How to read the particular SMS message in univarsal windows phone 10

In my project I want to read the particular SMS message and delete that message it possible in universal windows phone 10..

Is is posible using Chat Message Access in manifest file?

Upvotes: 1

Views: 465

Answers (1)

Vincent
Vincent

Reputation: 3746

You can use the chat messages api to access the SMS messages of your device. Using the ChatMessageStore you will be able to create/delete the messages but it might not be what you really want. The chat message api is more designed to create messaging application like WhatsApp.

If the message you want to receive is an app-directed message you can intercept it before it reaches the ChatMessageStore. The universal windows platform is exposing a new (restricted) API to intercept the messages before they reach the store using custom filtering rules. You can have a look at this sample. It is using the newest SmsMessageReceivedTrigger background task trigger.

Since this API is restricted, you will have to request the autorisation to use from Microsoft before being able to publish such an app to the store

Here is a sample about how to use the SmsMessageReceivedTrigger with the background task entry point and registration

public async void Run(IBackgroundTaskInstance taskInstance)
{
    var smsDetails  = taskInstance.TriggerDetails as SmsMessageReceivedTriggerDetails;

    // consume sms
    var from = smsDetails.TextMessage.From;
    var body = smsDetails.TextMessage.Body;

    // we acknoledege the reception of the message
    smsDetails.Accept();
}

static IBackgroundTaskRegistration Register()
{
    var taskNameAndEntryPoint   = typeof(SmsInterceptor).FullName;

    var task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(x => x.Name == taskNameAndEntryPoint);
    if(task != null) return task;

    var filterRule      = new SmsFilterRule(SmsMessageType.App);
    filterRule.SenderNumbers.Add("111111111");
    filterRule.SenderNumbers.Add("222222222");

    var filterRules             = new SmsFilterRules(SmsFilterActionType.AcceptImmediately);
    filterRules.Rules.Add(filterRule);

    var taskBuilder             = new BackgroundTaskBuilder();
    taskBuilder.Name            = taskNameAndEntryPoint;
    taskBuilder.TaskEntryPoint  = taskNameAndEntryPoint;
    taskBuilder.SetTrigger(new SmsMessageReceivedTrigger(filterRules));
    return taskBuilder.Register();
}

Since it is using a restricted API, you will have to add the following restricted capability to your appx manifest

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" 
    xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" 
    xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" 
    xmlns:r="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
    IgnorableNamespaces="uap mp r">
  <Capabilities>
    <r:Capability Name="cellularMessaging" />
  </Capabilities>
</Package>

You will find a complete sample here

If you want to use the ChatMessageStore API, you can have a look at this sample which should be a good start.

Upvotes: 1

Related Questions