Ways to guess if C2DM is connected

后端 未结 8 1441
花落未央
花落未央 2021-02-02 15:13

I\'m trying to place a best guess estimate as to whether C2DM messages can be received.

I\'ve created an application that relies on pushing information to a phone while

相关标签:
8条回答
  • 2021-02-02 15:29

    I don't think there's any way to determine in ADVANCE whether there's any chance a push attempt will work, but I can think of a fairly straightforward way to verify receipt (but not queueing for future delivery via C2DM) -- just complete the message loop.

    Remember, C2DM's main benefit is that it allows notifications when then phone is asleep and nominally offline. Once your application gets the notification, there's little to stop you from waking up the phone at that point, bringing up the network, and sending a confirmation. I don't think you'd even have to request "keep phone awake" permissions, because I believe the mere act of having registered for C2DM notifications and receiving one is sufficient to wake up the phone and allow the app to continue running normally (at least, long enough to bring up the network and send the confirmation).

    While you're at it, you should keep track of confirmations that happen LONG after you expected them to be a lost cause. If you see more than a few, you might have to alter the resend strategy.

    The only real-world edge case where this might fail is if you had users who bent over backwards to disable data while leaving voice/SMS enabled (I'm pretty sure C2DM uses 4 bytes of the response datagram sent when a phone polls for incoming calls & text messages that were originally set aside for RIM, then later repurposed for Apple and Google).

    0 讨论(0)
  • 2021-02-02 15:30

    I've worked a little with C2Dm, I've created my own push 3rd party server . I've implemented a little logic based oh C2DM http response code to know if a push message was sent or not . Here is some of the code I used :

    int responseCode = conn.getResponseCode();
    
        if (responseCode == HttpServletResponse.SC_UNAUTHORIZED || responseCode == HttpServletResponse.SC_FORBIDDEN) {
    
            LOGGER.warn("Unauthorized - need token");
    
            return false;
        }
    

    here I'm almost sure that the push message was sent from the c2dm servers because I've got an id ont the response:

    if (responseParts[0].equals("id")) {
            LOGGER.info("Successfully sent data message to device: " + responseLine);
    
            return true;
        }
    

    I've used other methods to get other result codes from Google if you want I can post them.I hope that I've helped you a bit .

    0 讨论(0)
  • 2021-02-02 15:31

    No that is impossible.As you device authenticated once and generate registration ID and send to third party server(As you already know).Now your work is over once the device has been registered.So Wait for message either you got or not(No guarantee of delivering message as C2DM used UDP Protocol ).

    Alternative Solution

    Although its impossible to check from Google side directly as i mentions above,But if you have any urgency to check connection from your phone then you can take approach like this

    Step 1): Make one Web service to check connection

    Step 2): Call this web service from application that will command to server to send push notification for checking purpose.

    Step 3): Now from server side,server will immediately send push notification for particular device(FROM which it get command)

    Step 4): Now if you got push notification that means you are still connected to C2DM.

    this will not take much time.But follow it only when checking connection is urgent and it is on user

    0 讨论(0)
  • 2021-02-02 15:35

    This may be a bit naive as I am not an active C2DM user, but wouldn't it be possible to read

    /proc/net/netstat

    and see if there are any active TCP connections. If there aren't any, then C2DM can't possibly be working. You could also make this technique more versatile by forming a C2DM whitelist that you would expect to find (or maybe its possible to filter on a special C2DM port?)

    0 讨论(0)
  • 2021-02-02 15:38

    Go watch this video, it's a Google I/O talk about C2DM, how to use it and how it works. AFAIK, you can't know if it's connected or not. Probably most of the time they don't even know (until they have to deliver a message and fail).

    However, it is highly recommended (in the video as well) that you do not send important data through C2DM (as messages can get lost). The service should only be used as a "network tickle" (with a footprint as small as possible). Your application should be woken up by this tickle and it should start fetching the information it needs itself.

    Now, if you implement it this way, it should be easy to implement a polling mechanism. Since you already separated the "tickle" from the actual information retrieval, you can just trigger the retrieval every once in a while if there's no tickle.

    Something you can do to check whether C2DM is connected is something like a ping:

    1. Send message to phone via C2DM
    2. The app receives (or doesn't receive) the message and sends a "pong" back to your server
    3. The server waits for the "pong" for a predetermined amount of time (1-2 minutes, I'd say) before marking the device as "offline".

    Edit: relying on GTalk is not feasible. GTalk relies on C2DM just like your app, it doesn't have anything "extra". Also, GTalk is not present on all devices. I'm not sure how the GTalk app determines whether it's offline or not (it's not open source, unfortunately), but I'd guess it just tries to ping a server and fails.

    0 讨论(0)
  • 2021-02-02 15:44

    If the device is inaccessible, even your fallback push messaging system wouldn't work. The C2DM doesn't guarantee that it will deliver your message, but the event of non-delivery would be very rare. So would be the case with any other service. The best workaround that you could have is to poll your server to check if you have any new messages that hasn't been delivered yet. I am assuming that your application is such that it's very important not to miss even a single message in 500 or may be 1000. In that case, you could implement a hybrid of push and pull.

    0 讨论(0)
提交回复
热议问题