Skip to content

N900 IM style notifications from python

May 22, 2010

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)
n.show()

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 –dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_led_pattern_activate 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 = ‘com.nokia.mce’
_MCE_REQUEST_PATH = ‘/com/nokia/mce/request’
_MCE_REQUEST_IF = ‘com.nokia.mce.request’
_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.

About these ads
9 Comments leave one →
  1. May 23, 2010 10:57 am

    Great find. I dug a bit deeper and I think you might appreciate the following:

    # activate led-pattern
    note.set_hint(“led-pattern”, “PatternCommunicationEmail”)
    # dbus callback
    note.set_hint(“dbus-callback-default”, “se.frals.fmms /se/frals/fmms se.frals.fmms open_gui”)

    … just need to figure out how to get the correct sound and vibration now :-) (there’s a hint called sound-file, fwiw)

    • danielwould permalink*
      May 23, 2010 11:50 am

      ah interesting, so you can put a ‘hint’ on a notification to get the led flashing, well that’s nice an obscure.
      I had foolishing thought that I could register a callback that was an actual method in my code, but notify doesn’t seem to work that way, at least I never get the callback. but it looks like if I write some dbus code to recieve signals that way, then I can call back to that. I guess that’s another load of reading/investigation I should be doing. Thanks for the tips

  2. May 24, 2010 1:14 pm

    Awesome! Can you add this somewhere on wiki.maemo.org (linking to this blog post as a source + posting the example code in the Wiki) where Python development is discussed, so that developers looking for this exact kind of information have an easier time finding it?

    • danielwould permalink*
      May 24, 2010 2:46 pm

      I’ll see if I can find somewhere appropriate. to be honest I so rarely find the info I’m looking for there, even through google searches, that I forget about it.

  3. May 24, 2010 2:12 pm

    I’m sure you know, but the notification is missing the icon. If you need the code for this – I’m sure we can scrape it up for you. I know that nowplayingd has a customized icon and also allows the notification to click through to the media player. Would be a good place to find info if you need it.

    • danielwould permalink*
      May 24, 2010 2:41 pm

      I tried adding an icon, but for some reason it didn’t display, so it would be cool to see a working example. I’ll check out nowplayingd thanks!

      • May 25, 2010 12:53 am

        Third argument to pynotify.Notification() is the icon, I never got it to work with an absolute filepath but if you got your application icon installed in /usr/share/icons/hicolor/(scalable|48×48|32×32)/hildon (not sure which one it takes, my guess is scalable or 48) you can just reference it by e.g. “witter” if the icon filename is witter.png.

        How I implemented it in fMMS:
        http://gitorious.org/fmms/fmms/blobs/master/wappushhandler.py#line104

Trackbacks

  1. Desktop notification support in gPodder | Maemo Nokia N900

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 99 other followers

%d bloggers like this: