user782104
user782104

Reputation: 13555

Click on notification to enter my app in android

Currently I am working on GCM (Google Cloud message), it allow user to push the message to user device. And I would like achieve the following requirement :

  1. if the user has already enter app , ignore it

  2. if the user has not enter the app , click on notification to enter the app

And the work flow of my app is:

  1. WelcomePage (download json and create data set from it) => MainPage (Display base on the data set)

The code to handle notification

private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        String notifyMsg = "";
        JSONTokener tokener = new JSONTokener(msg);

        if (tokener != null) {
            try {
                notifyMsg = new JSONObject(tokener).getString("msg");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Intent myintent = new Intent(this, WelcomePageActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getResources().getString(R.string.notification_title))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(notifyMsg))
        .setContentText(notifyMsg)
        .setContentIntent(contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

The problem is if I use WelcomePageActivity class , it will create a new activity if I am at the main page, how can I adjust the code to fit my requirement ?

Thanks

Upvotes: 7

Views: 1989

Answers (4)

ASP
ASP

Reputation: 1974

1.Create an object in GcmIntentService

public static final Object CURRENTACTIVIYLOCK = new Object();
  //for storing current activity
    public static Activity currentActivity;

2.Update this object value in onPause and onResume of MainActivity to recognize Activity is running or not.

@Override
public void onResume() {
    super.onResume();
    System.out.println("onResume Home page");
    synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
        GcmIntentService.currentActivity = this;
    }
}

@Override
public void onPause() {
    super.onPause();

    synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
        GcmIntentService.currentActivity = null;
    }
}

3.In GcmIntentService class, check for the current activity in onHandleIntent method.

synchronized (CURRENTACTIVIYLOCK) {
if (currentActivity != null) {
    if (currentActivity.getClass() == HomePageActivity.class) {

    } else {
        sendNotification(extras.getString("message"));
    }
} else {
        sendNotification(extras.getString("message"));
}

I'm sure this will help you.

Upvotes: 1

Vijju
Vijju

Reputation: 3508

You will not able to receive any notification click event so,

try this code :

Intent myintent = new Intent(this, TestActivity.class);
    myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(getResources().getString(R.string.notification_title))
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notifyMsg))
    .setContentText(notifyMsg)
    .setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

public class TestActivity  extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // check for your app state is running or not
    if(appRunning == false) {
        // start your WelcomePage activity.
    }
}

}

Upvotes: 2

Pararth
Pararth

Reputation: 8134

For
1. if the user has already enter app , ignore it:
in the onReceive() , check if your app is running, do not notify.
It can be checked with something like:

ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);

if((serviceList.size() > 0)) {
    boolean found = false;

    for(int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;

        if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
            //Your service or activity is running
            break;
        }
    }  
  1. if the user has not enter the app , click on notification to enter the app
    from the code above, you'l know if you would like to resume the app or launch - call Splash Screen or in your case WelcomeActivity.

About the workflow of your app, i'd suggest check whether you need to download the data every time or not. Can save it maybe or update/download only when required, and rest of flow works as it is.

Upvotes: 5

rarp
rarp

Reputation: 1122

In your AndroidManifest.xml, define your WelcomePageActivity with the flag android:launchMode="singleTop". From the definition of this flag:

A new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.

So with this flag, your activity will not be created again, rather it will receive a call in the onNewIntent() function with the Intent you used to create the PendingIntent for the notification. You could override this function, and use the intent to pass the activity new information.

Upvotes: 5

Related Questions