Search code examples
angularjsfirefox-os

addEventListner for received SMS using mozMobileMessage in B2G


I have been teaching myself how to write apps in AngularJS for Firefox OS for a university assignment. This is my first attempt at writing an app in JS.

The aim of the app is to run commands based upon 'commands' sent via SMS (aka, 'ring loud', 'lock device', 'turn on wifi', and reply to an SMS command with GPS locations). At the moment, I'm just trying to do a simple $window.alert('Messaged Received'), to display when an SMS has been received.

My problem is, I am trying to create an addEventListener for incoming SMS, and for the moment, display an $window.alert(). Later, I will use a case switch.

I've referred to the MDN API to create the Event Listener.

var mozMM = navigator.mozMobileMessage;
mozMM.addEventListener('received', function addEventListener(evt) {
    // Display a alert when a message is received
    $window.alert('SMS received');
    $window.alert(evt.message.body);
}, false);

My manifest.webapp has been set up to include all the relevant settings:

"type" : "certified",
"permissions": {
    "backgroundservice":{},
    "sms":{},
    ...
},
"messages": [
    { "sms-received": "/index.html" },
    { "notification": "/index.html" }
]

I am able to send SMS from my app without any issues. So I assume I have no problems with my permissions. I've also confirmed that I have full access to the mozMobileMessage object.

For testing, I am using a Geekphone, which has been rooted, and can install certified apps.

I have uploaded the source code to github: https://github.com/s3069246/findmydevice/tree/master/app


Solution

  • Thanks to someone on Google Groups for giving me the correct solution. I thought I would also share it here.

    The issue was that I was using the wrong Event Handler. I should have been using a system message handler instead

    navigator.mozSetMessageHandler('sms-received', function onSMS(sms) { 
        /* here your code */ 
    }); 
    

    Combined with the "message": [] handler in the manifest, the system handler will alert the application that a message has been received, even if the app is running in the background.