Notification in status bar binds the intent with the activit

Put your problem here if it does not fit any of the other categories.

Notification in status bar binds the intent with the activit

Postby araja » Thu Mar 25, 2010 11:57 am

I have created an activity which sends a number of notifications to status bar. Each notification contains an intent with a bundle. Here is the code:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      String ns = Context.NOTIFICATION_SERVICE;
  2.  
  3.         NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  4.  
  5.         int icon = R.drawable.icon;            
  6.  
  7.         long when = System.currentTimeMillis();
  8.  
  9.         Notification notification = new Notification(icon, "Test Notification", when);
  10.  
  11.        
  12.  
  13.                
  14.  
  15.         Context context = getApplicationContext();             
  16.  
  17.                
  18.  
  19.         Bundle bundle = new Bundle();
  20.  
  21.         bundle.putString("action", "view");
  22.  
  23.         Intent notificationIntent = new Intent(this, MainActivity.class);
  24.  
  25.         notificationIntent.putExtras(bundle);
  26.  
  27.                        
  28.  
  29.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
  30.  
  31.         notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);            
  32.  
  33.         mNotificationManager.notify(1, notification);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



When user clicks this notifications, I read the bundle string "action" and performs that action. Here is the code:



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  Bundle bundle = this.getIntent().getExtras();
  2.  
  3.                        
  4.  
  5.         if(bundle != null)
  6.  
  7.         {
  8.  
  9.                 String action = bundle.getString("action");
  10.  
  11.                 performAction(action)
  12.  
  13.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Everything works as expected. But, when I minimize the app using "arrow" button on device and then press and hold home button and clicks on my app icon the application starts and performs the same last action which have been performed by clicking the last notification. I figured out that when we click the app icon the application starts with last intent triggered by the notification. Can anyone help in this?
[syntax="java"]
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Top

Re: Notification in status bar binds the intent with the act

Postby pskink » Thu Mar 25, 2010 12:40 pm

araja wrote:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
[/syntax]


each time you obtain PendingIntent try different requestCode
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Thu Mar 25, 2010 12:46 pm

Thanks for quick reply.
Please clarify what do you mean by requestCode here?
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Thu Mar 25, 2010 12:59 pm

araja wrote:Thanks for quick reply.
Please clarify what do you mean by requestCode here?


see PendingInttent#getActivity docs
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Thu Mar 25, 2010 3:30 pm

I am now using different requestCode with each notification intent, but still observing the same behavior.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Thu Mar 25, 2010 3:53 pm

ok, i misunderstood your problem i thought that when calling notification from the statusbar you see previous data...

what you observe is ok - your application doesn't start from the scratch so you see the same data - if its not what you want you should clear your data in onPause/onResume or something
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Postby araja » Thu Mar 25, 2010 4:14 pm

So, now I have implemented this method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. protected void onPause() {
  2.  
  3.                 // TODO Auto-generated method stub
  4.  
  5.                 super.onPause();
  6.  
  7.                 this.getIntent().removeExtra("action");
  8.  
  9.                 this.setIntent(null);
  10.  
  11.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


But still if If I minimize the app and restore it, the last intent fires again.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby araja » Thu Mar 25, 2010 4:15 pm

Also, every time I restores the app, the onCreate method calls. This means app is being restarted.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Thu Mar 25, 2010 4:45 pm

araja wrote:Also, every time I restores the app, the onCreate method calls. This means app is being restarted.


hmmm, is onNewIntent called also?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Thu Mar 25, 2010 4:46 pm

I am not sure, but as per my observations, the PendingIntent will fire on activity start, no matter the contained notification is clicked or not. Can you confirm?
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby araja » Thu Mar 25, 2010 4:46 pm

No, onNewIntent never called.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby araja » Thu Mar 25, 2010 4:54 pm

No, I was wrong, if I do not click any notification and just minimize and restore the app, the PendingIntent does not trigger.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Thu Mar 25, 2010 5:08 pm

i'm not sure but maybe FLAG_ACTIVITIY_NEW_TASK is wrong flag?

try FLAG_ACTIVITIY_SINGLE_TOP or something
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Thu Mar 25, 2010 6:17 pm

I have tried different flags and combination of flags, but no success.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Thu Mar 25, 2010 8:29 pm

so i think the only solution is to store some flag in SharedPreferences and access it in onCreate to clear the data
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests