user2732743
user2732743

Reputation: 29

How to open specific Activity on Click of Push Notification

I am developing an android app in which application i am adding notification service using GCM. The problem is that i want to open specific activity on click of notification. I don't Know how to do this task.I also want to change the changes on server side and client side. The activity name be provided by the server and then when i click on the notification the activity opens. please help me .

I've tried this code.

protected void onMessage(Context context, Intent intent) {
        Log.i(TAG,
                "Received message. Extras:*************************************************** "
                        + intent.getExtras());
        // String message = getString(R.string.gcm_message);
        // displayMessage(context, message);
        // notifies user

        String inAction = intent.getAction();
        ArrayList<Notify> notifyData = new ArrayList<Notify>();
        if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {

            String json_info = intent.getExtras().getString("data");
            Notify notify = new Notify();
            if (json_info != null) {
                try {

                    JSONObject jsonObj = new JSONObject(json_info);
                    notify.setdetail(jsonObj.get("Detail").toString());
                    notify.setappUpdate(jsonObj.get("App Update").toString());
                    notify.setcontentUpdate(jsonObj.get("Content Update")
                            .toString());
                    notify.setSpecial(jsonObj.get("Special Event").toString());
                    notify.setSpecialContent(jsonObj.get("splEvtDes")
                            .toString());
                    notifyData.add(notify);
                } catch (JSONException e) {
                    // do nothing
                    return;
                }
            } else {

                notify.setdetail(intent.getStringExtra("Detail"));
                notify.setappUpdate(intent.getStringExtra("App Update"));
                notify.setcontentUpdate(intent.getStringExtra("Content Update"));
                notify.setSpecial(intent.getStringExtra("Special Event"));
                notify.setSpecialContent(intent.getStringExtra("splEvtDes"));
                notifyData.add(notify);
            }

        }
        String message = null;
        if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
            createNotificationForAppUpdate();
        } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
            message = notifyData.get(0).getdetail();

            generateNotification(context, message);
        } else {
            System.out
                    .println("=-----------------------------inside else_________________________");
            message = notifyData.get(0).getSpecialContent();
            generateNotification(context, message);

        }

    }

Upvotes: 0

Views: 14950

Answers (5)

Yafet Birhanu
Yafet Birhanu

Reputation: 1

<intent-filter>
      <action android:name="android.intent.action.VIEW"/>
     <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

I got similar problem and the activity opens after I include "android.intent.action.VIEW".

Upvotes: 0

Dilip
Dilip

Reputation: 2301

According to your example code,seems you are trying to perform different action in case of Appupdate, contentUpdate and Special content.So according to your code create three method.first for AppUpdate,2nd for content Update and last one for specialcontent.

         String message = null;
                if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
                    createNotificationForAppUpdate();
    //This will notify for App update
                } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
                    message = notifyData.get(0).getdetail();
        //This will notify for Content updte
                    generateNotification(context, message);
                } else {

//This will notify for special content
                    message = notifyData.get(0).getSpecialContent();
                    generateNotification(context, message);

                }

Method for AppUpdate

private void createNotificationForAppUpdate()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("Enter here playstore link"));
startActivity(browserIntent);
}

Method for Content Notification.

 private void generateNotification(Context context,String message){
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(context, YourActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(context, 0, viewIntent, 0);

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle("Title")
        .setContentText(message)
        .setContentIntent(viewPendingIntent);

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(context);

// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
 }

Accordingly create method for special too. Hope this will help GOOD LUCK

Upvotes: 2

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

Mention you specif acitivity (here MainActivity.class) in Pending Intent like this

Intent notificationIntent = new Intent(context, MainActivity.class);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

Upvotes: 1

Rohitashv jain
Rohitashv jain

Reputation: 244

private void notificationMethod(){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(yourActivity.this)
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle("title")
                .setContentText("text");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(yourActivity.this, wantToOpenActivity.this);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(yourActivity.this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack( wantToOpenActivity.this);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        int mId=001;;
        mNotificationManager.notify(mId, mBuilder.build());
    }

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

You can specify any activity to be receiver for push notifications:

<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

This intent filter for the activity specifies which activity will be launched in response to push notification (PACKAGE_NAME is your Android app package)

So you can add this intent filter in your activity which you want to open on the click of Push notification.

Referenec: More Info

Upvotes: 1

Related Questions