locked
Unable to get a specific message from SMS message store with getMessageAsync(id)

    Question

  • Hi!

    I'm trying to get a specific message from SMS messageStore with the getMessageAsync(id) method. What I'm doing now is:

    1. I'm having a background task which is triggered when a new SMS is received
    2. The background task passes deviceId and messageIndex to the foreground app
    3. The foreground app creates SMSDevice instance using the fromIdAsync(deviceId) method
    4. Then I'm trying to get the content of the received message by accessing device's messageStore and using getMessageAsyn(messageIndex) function

    What then happens is that no errors are raised, but the asynchronous getMessage method never goes to the .then callback function.

    Any ideas what could be the problem?

    I'm able to use the SMS device otherwise (sending/receiving message using the code from the SMS Send/Receive sample) and I can receive the message body when I'm listening the "onsmsmessagereceived" event. Because I have to run the application on background, I've understood that I should use the background task rather than listening the "onsmsmessagereceived" event.

    I'm using JavaScript/HTML5.

    Thanks

    -Mikko

    PS. Sorry I'm unable to paste the actual code here, but if needed, I'll do that as soon as I'm back on my work computer.

    Tuesday, October 25, 2011 6:22 AM

Answers

  • A couple of things,

    1. Can you uncomment the following line at the beginning of default.js, we want to know if there are any other errors happening?

    Debug.enableFirstChanceExceptions(true);

    2. Can you tell us how you are storing the message index in the background task?  The index shouldn't always be 1, it should keep increasing until the sim max capacity.         

    thanks

    Frank                         

    • Marked as answer by Mikko Koski Friday, October 28, 2011 6:13 AM
    Wednesday, October 26, 2011 10:11 PM
    Moderator
  • Hi Frank!

    Thanks for the reply! After banging my head against the wall, I actually managed to get the messageStore working when I ran the piece of code on my ExoPC tablet. That gave me a little bit hope although I was still unable to get it working on my development laptop. 

    I added the enableFirstChanceExceptions as you suggested and when I ran the app, it in fact raised an error I didn't have seen before. Before adding the background task, I'm cleaning up all the old background tasks and the error was raised when I tried to unregister an old task:

    task.unregister(true);
    


    I started thinking that there might be some old tasks messing up the whole thing and thus restarted my laptop. And of course, after that it worked all fine... D'oh.

    So, it seems that this might be problem just in my own development environment...

     

    Thanks for all the help!

    • Marked as answer by Mikko Koski Friday, October 28, 2011 6:13 AM
    Thursday, October 27, 2011 6:58 AM

All replies

  • Here's the piece of code that is giving me a headache:

    smsTask.addEventListener('completed', function () {
        var settings = Windows.Storage.ApplicationData.current.localSettings;
        var messageIndex = settings.values[smsTask.taskId + '.messageIndex'];
        var deviceId = settings.values[smsTask.taskId + '.deviceId'];
    
        try {
            var smsDevice = Windows.Devices.Sms.SmsDevice.fromId(deviceId);
            var promise = smsDevice.messageStore.getMessageAsync(messageIndex).then(
            function (result) {
                debugger;
                console.log("Success", result);
            },
            function (error) {
                debugger;
                console.log("Error", error);
            },
            function (progress) {
                debugger;
                console.log("Progress", progress);
            });
    
        } catch (err) {
            console.error(err);
        }
    });
    

    smsTask is the background task which is successfully triggered when a new SMS is received. 

    What values should the messageIndex have? For me the value of messageIndex has always been 1. Might that be the problem?

    Wednesday, October 26, 2011 7:03 AM
  • A couple of things,

    1. Can you uncomment the following line at the beginning of default.js, we want to know if there are any other errors happening?

    Debug.enableFirstChanceExceptions(true);

    2. Can you tell us how you are storing the message index in the background task?  The index shouldn't always be 1, it should keep increasing until the sim max capacity.         

    thanks

    Frank                         

    • Marked as answer by Mikko Koski Friday, October 28, 2011 6:13 AM
    Wednesday, October 26, 2011 10:11 PM
    Moderator
  • Hi Frank!

    Thanks for the reply! After banging my head against the wall, I actually managed to get the messageStore working when I ran the piece of code on my ExoPC tablet. That gave me a little bit hope although I was still unable to get it working on my development laptop. 

    I added the enableFirstChanceExceptions as you suggested and when I ran the app, it in fact raised an error I didn't have seen before. Before adding the background task, I'm cleaning up all the old background tasks and the error was raised when I tried to unregister an old task:

    task.unregister(true);
    


    I started thinking that there might be some old tasks messing up the whole thing and thus restarted my laptop. And of course, after that it worked all fine... D'oh.

    So, it seems that this might be problem just in my own development environment...

     

    Thanks for all the help!

    • Marked as answer by Mikko Koski Friday, October 28, 2011 6:13 AM
    Thursday, October 27, 2011 6:58 AM