maemo, top tip

N900 IM style notifications from python

In the last couple of days I've spent several hours trawling the internet in search of an example of something I hoped would be fairly easy. How to trigger an IM/Email type notification event from python. Many people have asked that Witter do these style of notifications when a mention or DM is received. But how to do them? I had been using:note = osso.SystemNote(self.osso_c) result = note.system_note_infoprint(str(receive_count) + " Tweets Received") to display a banner, but I could find nothing that would point me towards the style which flashes the corner menu button and shows up in the task switcher until dismissed.

After a great deal of digging, I discovered pynotify and the syntax to do what I wanted n = pynotify.Notification("Witter","You have "+str(receive_count)+" new mentions") n.set_urgency(pynotify.URGENCY_CRITICAL)

This is very cool but only part of the job, as doing this does not also flash the led, or vibrate, or play a sound. I had perhaps foolishly assumed there were a combination of the same thing, an alert, but no, I have to figure them out separately.

First job was led flashing, and it was not simple. There are plenty of posts about manipulating the patterns being used by the n900 already, but it took me some time to find a page which showed the dbus commands that trigger the actual event. That said at time of writing I can't find that page again, even though I now know more of what I'm looking for. the command to use from x-terminal is dbus-send --system --type=method_call /com/nokia/mce/request string:PatternCommunicationIM

Simply putting deactivate instead of activate turns it off again. Ok so having gotten this far, all I needed to do is figure out how to send that signal from python....easy right?

I could not for the life of me find any example of using dbus like this, it took me ages to figure out that I needed to use rpc.rppc_run to send the request, and then the only examples I could find were for simple toggles of one mode functions, that is nothing that passed parameters such as the pattern to use. I got to the point where the code ran ok, but was clearly not passing the information required to actually turn the pattern on.

To cut an already long story short, I basically got down to trial and error throwing combinations of probable looking options as the rpc_run command, once I had found the rpc_args parameter, I still had to figure out quite what format to use, the command line example had string:PatternCommunicationIM, was the 'string:' value important? how to specify? I tried a bunch of stuff, originally just passing PatternCommunicationIM as a string, and getting errors about parameters needing to be a tuple. Ultimately I guessed, and just chucked some empty strings after the string I wanted to pass... and finally this is how to do it

I have the following static strings defined: _MCE_SERVICE = '' _MCE_REQUEST_PATH = '/com/nokia/mce/request' _MCE_REQUEST_IF = '' _ENABLE_LED = 'req_led_pattern_activate' _DISABLE_LED = 'req_led_pattern_deactivate' _VIBRATE = 'req_vibrator_pattern_activate' _VIBRATE_PATTERN= 'PatternChatAndEmail' _LED_PATTERN = 'PatternCommunicationIM'

then these 3 lines enable the led and trigger a vibration:

rpc = osso.Rpc(self.osso_c) rpc.rpc_run(self._MCE_SERVICE, self._MCE_REQUEST_PATH,self._MCE_REQUEST_IF,self._ENABLE_LED,rpc_args=(self._LED_PATTERN,"",""),use_system_bus=True) rpc.rpc_run(self._MCE_SERVICE, self._MCE_REQUEST_PATH,self._MCE_REQUEST_IF,self._VIBRATE,rpc_args=(self._VIBRATE_PATTERN,"",""),use_system_bus=True)

All in all it's not that hard, there is not much code there. If I wasn't defining the strings as constants then it would literally be 3 lines of code to set the LED and vibrate. I thought I'd have to use a callback to turn the LED pattern off again, but it turns out it does that automatically when I click on the event. Which is just as well because I haven't gotten the callback to work yet. so at the moment clicking on the event just dismisses it, it doesn't take you to the appropriate view in witter (like an e-mail event does) However I thought this was worth sharing in the hopes that anyone else that wants to do this kind of even notification doesn't have to go through the same pain as me trying to figure it out.