Alan Lai
Alan Lai

Reputation: 1104

Confuse of Push Notification in Android

I followed this Push Notification tutorial.

When I finish the tutorial, I found out that two classes did not use which were AuthenticationUtil and MessageUtil.

Moreover, Google Login, this link seem unworkable. Second, This is the token id for the Android device or the account only? i thought push notification is push message to token id of Android device.

On the others hand, i found out that the bundle.putExtra(key, value), all the keys did not use it. For example put "app" but in C2DMRegistrationReceiver() did not get the key.

In this sendRegistrationIdToServer(), it seem like never being call out.

I am being confused by this tutorial about push notification.

Who can guide me or give me workable tutorial or example about push notification?

I would like pro to point out what's my wrong.

This is my registration id

public static final String[] REGISTRATION_ID = {
        "APA91bFV6MwoAH0UNop69PZ2liKpSBUHSHenIuPzh44_6GdGKzVCLvoH_NM31eMZMVLZi-SAIFwP4iZaE72dSWkIh3GaD0RQYpPm9zO0ARWmnoxFyyyreL_KpQ9Qd_p0broclT12RhA4Ymk0cBT00CmpsbSHIwyxig",
        "APA91bEwmxgvs7zNbKC4p0n4DoTEM73DTihnQgBOP8Gxhf2sVW-fgltugDgS1Fh2S4KvN1wQHbMNJEIzieJ9F1nNPqs3NWeKGbB7IBYpKJq4xmN4Z7uzkjZQQUKGD8jW--AwfQY5McINBto9GAL_87_u5WkIq-kx3g",
        "APA91bH63Zgxn1X_MZ56UzrlRpffvmiLAIsqxvBUTMUHP2O_MT_VU9Ork_edXKHlml-PZSkjKEqdk8EKv5HvxbPdK1Vva3WtmqsPZfhXzEbtNIrwrqIvvRf7hL835rDc4t2E8EKUBj1dX2ta0OxY5pY3Xlhkyb1sBg",
        "APA91bGqT5Wo6eUaMdqT5r9TlGbKSX6GN2W6r-RjrRXz5T5v3j87flcQRyfSajmMNGXuPVe-fwZydRmvyYu63tWnYohDmpJyKkXOxs8Vx6P_FplFQ__ufR_hekwqGOspeUc6bfc8fhbMPGN3Ft9l-bfrghJwwk79jw"};

Messageutil

public static int sendMessage(String auth_token, String registrationId,
        String message, String title) throws IOException {

    StringBuilder postDataBuilder = new StringBuilder();
    postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
            .append(registrationId);
    postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
            .append("0");
    postDataBuilder.append("&").append("data.payload").append("=")
            .append(URLEncoder.encode(message, UTF8));
    postDataBuilder.append("&").append("data.title").append("=")
            .append(URLEncoder.encode(title, UTF8));

    byte[] postData = postDataBuilder.toString().getBytes(UTF8);

    // Hit the dm URL.

    URL url = new URL("https://android.clients.google.com/c2dm/send");
    HttpsURLConnection
            .setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");
    conn.setRequestProperty("Content-Length",
            Integer.toString(postData.length));
    conn.setRequestProperty("Authorization", "GoogleLogin auth="
            + auth_token);

    OutputStream out = conn.getOutputStream();
    out.write(postData);
    out.close();

    int responseCode = conn.getResponseCode();
    return responseCode;
}

private static class CustomizedHostnameVerifier implements HostnameVerifier {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

Messagesender

public static void main(String[] args) throws IOException {
    String token = AuthenticationUtil.getToken(SecureStorage.USER,
            SecureStorage.PASSWORD);

    for (int i = 0; i < ServerConfiguration.REGISTRATION_ID.length; i++) {
        MessageUtil.sendMessage(token,
                ServerConfiguration.REGISTRATION_ID[i], "12358",
                "印尼羽賽:馬2單1雙止步入選賽");
        System.out.println(ServerConfiguration.REGISTRATION_ID[i]
                .toString());
    }
    System.out.println(token);

}

Upvotes: 1

Views: 2205

Answers (3)

chiranjib
chiranjib

Reputation: 5308

Google Cloud Messaging for Android

Important: C2DM(Android Cloud to Device Messaging Framework) has been officially deprecated as of June 26, 2012. This means that C2DM has stopped accepting new users and quota requests. No new features will be added to C2DM. However, apps using C2DM will continue to work. Existing C2DM developers are encouraged to migrate to the new version of C2DM, called Google Cloud Messaging for Android (GCM). See the C2DM-to-GCM Migration document for more information. Developers must use GCM for new development.

Kindly check the following link:

http://developer.android.com/guide/google/gcm/index.html

Upvotes: 1

Samdrain
Samdrain

Reputation: 433

You should follow this tutorial for android c2dm implementation.

For server, you could use anything, some code sample available on internet. For server I used .NET library called "C2DM Sharp"

The process is very simple like...

  • First register your google email for c2dm on - https://developers.google.com/android/c2dm/signup
  • Run the android application on Android 2.2 or higher and send the registrationID which you can get in "C2DMReceiver" or get that ID by writting in LOG
  • Use the server code, for testing purpose paste your registrationID in Server code and you are ready to go.

The basic flow of C2DM is ... Register Phone for C2DM -> Get registrationID -> Send registrationID to server -> Server usees google id to get auth token -> server use registrationID and auth token to send message.

Upvotes: 1

katit
katit

Reputation: 17905

Please see my question here:

C2DM server. Should we use OAuth now?

There is some info and link to google group with answer.

In short..

Seems like OAuth2 will work, but I didn't find any working sample to implement

Client Login works and this is place where my confusion was. You need to:

  1. Set up google account. I picked something like [email protected].
  2. Register for C2DM using this email. This is important.
  3. On server-side use email/password you setup to get auth token.
  4. Use this token to send messages from server.

Everything else is just like in all tutorials around.

Upvotes: 0

Related Questions