Android to PC notifications platform

2019-06-07 06:23发布

问题:

I want to write an Android app that would send notifications from the mobile device to a client application installed on a PC. The notifications would contain data that would be parsed by the client. The notifications are one way only.

Or in other words: "Send data notifications from one device to another, sharing the same user account"

What would be the best notifications/messaging platform to implement such behavior?

Some requirements:

  1. Use existing [free] server infrastructure (gtalk?)
  2. Mobile-PC connection has to be set by common server authentication (no broadcast or by ip)
  3. Notification timing has to be near real time
  4. Simple to implement
  5. [Optional] Use Google account authentication

Any constructive feedback about my requirement would be appreciated as well.

回答1:

It sounds like XMPP would fit your requirements. It would certainly allow someone to use their Google Talk account or any other XMPP account (e.g. jabber.org), as long as your 'notifications' are reasonable in size and frequency (these are free services after all!).

You should be able to accomplish this fine using existing libraries. This post will discuss things at the basic XMPP level, but many libraries will provide higher level APIs for doing the things described here.

Some notes on how to achieve various things you want:

Every connection to an XMPP account is assigned a unique identifier called a 'resource'. In XMPP a simple address like user@gmail.com is called a 'bare JID'. You can also send to a specific connection by including the resource, user@gmail.com/your-app829abc (a 'full JID'). Because resources come and go, and can vary (Google semi-randomizes them for example), presence is used to broadcast availability and unavailability of resources.

The desktop client needs to send presence, so that the mobile client can see it online. It should also include a priority of '-1' in its presence, to prevent it receiving normal chat messages from the user's contacts. It should also include capabilities or similar identifying information so that the mobile client can identify it apart from other apps, such as IM clients, online with the user's account.

One thing to note, that may or may not be what you want, there is no way for the desktop client to appear offline. It obviously needs to send presence, so that the mobile client can locate it, but the user's contacts will also see it online (even if the user is not signed into their IM client). The negative priority will prevent it receiving IM messages, however.

<!-- Desktop sends: -->
<presence>
    <priority>-1</priority>
    <your-app xmlns="http://example.com/your-app" type="desktop" />
</presence>

So now from the mobile client's perspective... it needs to simply connect to the same account, and also send similar presence. After sending its own presence it will automatically receive the presence from contacts and also other connections to the same account, including the desktop client's connection.

<!-- Mobile sends: -->
<presence>
    <priority>-1</priority>
    <your-app xmlns="http://example.com/your-app" type="mobile" />
</presence>

<!-- Mobile receives (among other things): -->
<presence from="user@gmail.com/foo38Bc21e">
    <priority>-1</priority>
    <your-app xmlns="http://example.com/your-app" type="desktop" />    
</presence>

Now you have the desktop client's full JID.

It can send a normal XMPP message direct to the desktop client's full JID, as seen in the 'from' of the presence packet. In XMPP IM messages use the <body> element in the message to convey text, however you can omit the <body> tag and insert your own XML data:

<!-- Mobile sends: -->
<message to="user@gmail.com/foo38Bc21e">
    <your-notification xmlns="http://example.com/your-app">
        <any-xml-here/>
    </your-notification>
</message>

You'll be able to receive this message on the desktop almost immediately (the greatest latency will generally be in your mobile network). For XMPP on mobile, also take a look at XEP-0286: XMPP on Mobile Devices.