简体   繁体   中英

Opening an Activity from a notification -Android

I am using GCMBaseIntentService in my service class which extends GCMBaseIntentService I override the method onMessage(Context,Intent) . The code goes here :

protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");

    //String message = getString(R.string.gcm_message);
    String id=intent.getExtras().getString("event_id");
    String message=intent.getExtras().getString("title");
    String eventname=intent.getExtras().getString("event_name");

    generateNotification(getApplicationContext(), id, message, eventname);  
}

private static void generateNotification(Context context, String id, String message, String eventname) 
{
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

   //new intent
    Intent notificationIntent = new Intent(context, EventDescription.class);
    notificationIntent.putExtra("event_id", id);//need this id in the eventdescription activity

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

    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, eventname, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification); 
}

The problem is that, while I click this notification and in the Eventdescription activity and extract the extras from intent and print the ID. It shows the correct value only the first time, after that for every notification it just shows the 1st value all the time. Please help!

Try this

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

Please note requestCode of the getActivity of Pending intent should be unique.So just pass

unique value for every notification.You can put time stamp or even the id which you receive

from the server. I have tried this and this works.Do share your feedback

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notificationsfinal);


    start=1;
    end=15;

    next=(Button)findViewById(R.id.nextbtn);
    prev=(Button)findViewById(R.id.prevbtn);
    statusview=(TextView)findViewById(R.id.status);


    notification_list_view=(ListView)findViewById(R.id.not_listview);
    updatestatus();
    getNotifications();

}
private void getNotifications()
{
    eventnotifierapp app=(eventnotifierapp)getApplication();



    id=getIntent().getExtras().getString("event_id");
    db=new MyDB(this);
    }

This is my EventDescription activity where i am retreiving this event_id

Your subsequent Intent s aren't different enough, so they're being recycled. Specifically, changing the extras doesn't make a distinct Intent from the perspective of the system.

The docs for Intent.filterEquals explain that you will need to change one or more of: action, data, type, class, and categories. One approach, therefore, is to stick some distinguishing information (in your case the event_id ) into the data URI; you don't have to use this URI for anything but it ensures that your intents will be distinct.

There's more info in this related question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM