Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 240
user782104 Avatar asked Feb 27 '14 03:02

user782104


2 Answers

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.

like image 63
Pararth Avatar answered Sep 23 '22 07:09

Pararth


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.

like image 30
rarp Avatar answered Sep 20 '22 07:09

rarp