Click on the notification to enter my Android app.

I am currently working on GCM (Google Cloud message), it allows the user to click the message on the user device. And I would like to fulfill the following requirement:

  • if the user has already entered the application, ignore it

  • If the user has not logged into the application, click on the notification to enter the application

And the work flow of my application:

  • WelcomePage (loading json and creating a dataset from it) => MainPage (display base in the dataset)

Notification Processing Code

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 that if I use the WelcomePageActivity class, it will create a new action, if I am on the main page, how can I customize the code according to my requirement?

thanks

+7
android android-intent push-notification notifications android-notifications
source share
4 answers

For
1. If the user has already launched the application, ignore it:
in onReceive() , check if your application is working, do not notify.
It can be checked using 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; } } 
  • If the user has not logged into the application, click on the notification to enter the application
    from the above code you will find out whether you want to restart the application or start Splash Screen or in your case WelcomeActivity.

About the workflow of your application, I would suggest checking if you need to upload data every time. It can save it, maybe, or update / load only when necessary, and the rest of the stream works as it is.

+5
source share

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

A new instance of the "singleTop" action can also be created to handle the new intent. However, if the target already has an action instance at the top of its stack, this instance will receive a new intent (in onNewIntent () ); no new instance created.

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

+5
source share

You won’t be able to receive click notifications, therefore

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. } } 

}

+2
source share

1.Create an object in a GcmIntentService

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

2. Set this object value to onPause and onResume of MainActivity to recognize the action. It works 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 the GcmIntentService class, check the current activity in the onHandleIntent method.

 synchronized (CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == HomePageActivity.class) { } else { sendNotification(extras.getString("message")); } } else { sendNotification(extras.getString("message")); } 

I am sure this will help you.

+1
source share

All Articles