Android: Push Notification
Asked Answered
T

1

0

I am developing an application. In that application i am using the GCM (Push Notification).

I took help of this Link 1.

and i successfully implemented the client side code.

Now to send notification i am using the following link as :

Link 2

But the issue is that i am ale to get the notification, but when i click on that nothing happens.

What should i do, so that if user click on notification web page or update page etc opens.

I used the same code as given in that link. No changes done so far now.

I am in middle of my app. So please guide me and give your valuable suggestions.

Troposphere answered 11/9, 2013 at 9:28 Comment(0)
L
0

this function is call in your main activity.

public void registerClient() {

        try {
            // Check that the device supports GCM (should be in a try / catch)
            GCMRegistrar.checkDevice(viewLogin);

            // Check the manifest to be sure this app has all the required
            // permissions.
            GCMRegistrar.checkManifest(viewLogin);

            // Get the existing registration id, if it exists.
            regId = GCMRegistrar.getRegistrationId(viewLogin);

            if (regId.equals("")) {

                registrationStatus = "Registering...";

                // register this device for this project
                GCMRegistrar.register(viewLogin, GCMIntentService.PROJECT_ID);
                regId = GCMRegistrar.getRegistrationId(viewLogin);

                registrationStatus = "Registration Acquired";

                // This is actually a dummy function. At this point, one
                // would send the registration id, and other identifying
                // information to your server, which should save the id
                // for use when broadcasting messages.

            } else {

                registrationStatus = "Already registered";

            }
            Log.d(TAG, regId);
            sendRegistrationToServer();
        } catch (Exception e) {

            e.printStackTrace();
            registrationStatus = e.getMessage();

        }

        Log.d(TAG, registrationStatus);


    }

public class GCMIntentService extends GCMBaseIntentService {

    public static final String PROJECT_ID = "566655788";
    private static final String TAG = "GCMIntentService";
    ModelNotificationMessage modelNotificationMessage;

    public GCMIntentService() {
        super(PROJECT_ID);
        Log.d(TAG, "GCMIntentService init");
    }

    @Override
    protected void onError(Context ctx, String sError) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Error: " + sError);

    }

    @Override
    protected void onMessage(Context ctx, Intent intent) {

        Log.d(TAG, "Message Received");

        String message = intent.getStringExtra("message");
        try {
            modelNotificationMessage = JsonParserNotificationMessage
                    .parserString(message);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d(TAG, "Message Received" + message);

        sendNotification(message);
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("GCM_RECEIVED_ACTION");

        broadcastIntent.putExtra("gcm", message);

        ctx.sendBroadcast(broadcastIntent);

    }

    private void sendNotification(String message) {
        // this
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification;
        CharSequence tickerText = message; // ticker-text
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = modelNotificationMessage.getKey();
        CharSequence contentText = message;
        Intent notificationIntent = null;

        int NOTIFICATION_ID = 9999;
        try {

                NOTIFICATION_ID = CommonVariable.notification_Limit;
                notificationIntent = new Intent(this, ViewLimit.class);

                contentText = "Limit received for " + modelAgents.getName()
                        + ".";
                tickerText = "Limit received for " + modelAgents.getName()
                        + ".";

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // and this
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    @Override
    protected void onRegistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send regId to your server
        Log.d(TAG, regId);

    }

    @Override
    protected void onUnregistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send notification to your server to remove that regId

    }

}

//json parser class

public class JsonParserNotificationMessage {

    private static final String KEY = "Key";
    private static final String BODY = "Body";

    public static ModelNotificationMessage parserString(String jsonStrng)
            throws JSONException {
        JSONObject jObject = new JSONObject(jsonStrng);
        ModelNotificationMessage modelNotificationMessage = new ModelNotificationMessage();
        if (jObject != null) {
            modelNotificationMessage.setKey(jObject.getString(KEY));
            modelNotificationMessage.setBody(jObject.getString(BODY));
        }
        return modelNotificationMessage;
    }
}

i hope it is useful for you... if you any query then tell me.this code suceessfully work in my app.

Louisville answered 11/9, 2013 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.