1

I am working on a GPS app. My app will fire notification by NotificationManager if the user go inside or outside a predefined zone. My app can fire notification for both cases.

From onResume(), I always get the latest intent.setExtra() value instead of of intent.setExtra value of the clicked notification.

The problem is how to detect the user click on the notification for inside zone or outside zone? (I want to show different view on different case)

Is it possible to add a listener for notification clicked?

Here is my code spinet:

private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
{       

    Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis());

    PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);

    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

    if (vibrate)
        notification.vibrate=new long[] {100L, 100L, 200L, 500L};

    if (playSound)
        notification.defaults |= Notification.DEFAULT_SOUND;


    notification.number = ++notificationCounter;
    notificationMgr.notify(notificationTag, notificationCounter, notification);

}

@Override
protected void onNewIntent( Intent intent ) {
    Log.i( TAG, "*********** onNewIntent(), intent = " + intent );
    if (intent.getExtras() != null)
    {
        Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
    }
    super.onNewIntent( intent );
    setIntent( intent );
}

@Override
public void onResume() {
    super.onResume();
    Log.i(TAG, "*********** Main - onResume()");

    Intent intent = this.getIntent();
    if (intent.getExtras() != null)
    {
        Log.i(TAG, "test = " + intent.getExtras().getString("test"));
    }
}

public void createNotification(String msnContent)
{
    Intent intent = new Intent();
    intent.setClass(this, Main.class);
    Bundle bundle = new Bundle(); 
    bundle.putString("test", msnContent);
    intent.putExtras(bundle);
    displayNotificationMessage(msnContent, true, true, intent, "test"); 

}
mobile app Beginner
  • 1,651
  • 3
  • 26
  • 40

2 Answers2

2

Is it possible to add a listener for notification clicked?

Use a different PendingIntent for each case. If you are only changing an extra, be sure to use FLAG_UPDATE_CURRENT when creating the PendingIntent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, I have modified my code. I pass a new PendingIntent object to notification.setLatestEventInfo() every time before I send the notification by notificationMgr.notify(). But it still cannot distinguish which notification at Notification Bar has been clicked. Since onResume() always return the latest setExtra() value. – mobile app Beginner May 24 '11 at 16:39
  • Is it any way to pass value/object from original activity to the intent called by Notification? – mobile app Beginner May 24 '11 at 16:42
  • @user767987: "Since onResume() always return the latest setExtra() value." See http://stackoverflow.com/questions/6089698/get-the-intent-that-woke-up-my-activity/6089739#6089739 – CommonsWare May 24 '11 at 16:47
  • Thanks again. I have read the link that you provided and add Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP to the intent that passes to PendingIntent. But I found that onNewIntent( Intent intent ) still get the latest intent that I passed to notification.setLatestEventInfo(). Anything goes wrong? or is there any other way to distinguish which notification at the Notification Bar has been clicked? – mobile app Beginner May 25 '11 at 01:47
  • @Android Beginner: "But I found that onNewIntent( Intent intent ) still get the latest intent that I passed to notification.setLatestEventInfo()." -- this is precisely what you want. – CommonsWare May 25 '11 at 10:37
  • Actually, I don't want to always get the latest intent that I pass to notification.setLatestEventInfo(). I want to get different intent for different Notification at Notification Bar. What I was trying to do is use the intent that passed to onNewIntent( Intent intent ) to distinguish which Notification has user clicked. – mobile app Beginner May 25 '11 at 14:06
  • @Android Beginner: Use `PendingIntents` that are substantially different (e.g., different action strings) for your two `Notifications`. Then, the `Intent` passed to `onNewIntent()` will tell you what you want to know. – CommonsWare May 25 '11 at 14:12
  • Thanks CommonsWare. I solved problem by adding intent.setAction() – mobile app Beginner May 25 '11 at 16:04
0

I found a solution from this post - Confusing behavior when starting new activity with PendingIntent. Also, thanks to the help from CommonsWare.

I was missing intent.setAction().

Below is the code spinet of detect which notification at the Notification Bar has been clicked. Hope it can help someone else who has the same problem.

package com.NotificationTest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
    private static final String TAG = Main.class.getSimpleName();
    int notificationCounter =0;
    private NotificationManager notificationMgr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        notificationMgr =(NotificationManager)getSystemService(
                NOTIFICATION_SERVICE);

        notificationMgr.cancelAll();

        Button b1 = (Button) findViewById(R.id.b1);
        b1.setText("B1");
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                createNotification("From B1");
            }
        });


        Button b2 = (Button) findViewById(R.id.b2);
        b2.setText("B2");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                createNotification("From B2");
            }
        });
    }


    private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
    {       

        Notification notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());


        contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);



        /*if (vibrate)
            notification.vibrate=new long[] {100L, 100L, 200L, 500L};

        if (playSound)
            notification.defaults |= Notification.DEFAULT_SOUND;*/


        notification.number = ++notificationCounter;
        notificationMgr.notify(notificationTag, notificationCounter, notification);

    }

    @Override
    protected void onNewIntent( Intent intent ) {
        Log.i( TAG, "*********** onNewIntent(), intent = " + intent );
        if (intent.getExtras() != null)
        {
            Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
        }
        super.onNewIntent( intent );
        setIntent( intent );
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "*********** Main - onResume()");

        Intent intent = this.getIntent();
        if (intent.getExtras() != null)
        {
            String extraOfClickedNotification = intent.getExtras().getString("test");

            Log.i(TAG, "test = " + extraOfClickedNotification);

            if (extraOfClickedNotification.equals("From B1"))
            {
                // Do something
            }
            else if (extraOfClickedNotification.equals("From B1"))
            {
                // Do something
            }
        }
    }

    public void createNotification(String msgContent)
    {
        Intent intent = new Intent();
        intent.setAction(msgContent + System.currentTimeMillis());
        intent.setClass(this, Main.class);
        Bundle bundle = new Bundle(); 
        bundle.putString("test", msgContent);
        intent.putExtras(bundle);
        displayNotificationMessage(msgContent, true, true, intent, "test"); 

    }

}
Community
  • 1
  • 1
mobile app Beginner
  • 1,651
  • 3
  • 26
  • 40