Notification in status bar binds the intent with the activit

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

Postby araja » Fri Mar 26, 2010 7:32 am

Even this flag will not help, because I need some way to differentiate between activity started by notification and activity started by minimizing and restoring.

Here is the complete code, you can try to run it and observe the behavior:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.src.notifications;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.Notification;
  9.  
  10. import android.app.NotificationManager;
  11.  
  12. import android.app.PendingIntent;
  13.  
  14. import android.content.Context;
  15.  
  16. import android.content.Intent;
  17.  
  18. import android.os.Bundle;
  19.  
  20.  
  21.  
  22. public class NotificationActivity extends Activity {
  23.  
  24.     /** Called when the activity is first created. */
  25.  
  26.     @Override
  27.  
  28.     public void onCreate(Bundle savedInstanceState) {
  29.  
  30.         super.onCreate(savedInstanceState);
  31.  
  32.         setContentView(R.layout.main);
  33.  
  34.        
  35.  
  36.         Bundle bundle = this.getIntent().getExtras();
  37.  
  38.                
  39.  
  40.                 if(bundle != null)
  41.  
  42.                 {
  43.  
  44.                          String page_notifications = bundle.getString("page");
  45.  
  46.                          System.out.print(page_notifications);
  47.  
  48.                 }
  49.  
  50.                
  51.  
  52.         showStatusBarNotification(1, "test message", "face2face", "test message", "test_url");
  53.  
  54.        
  55.  
  56.     }
  57.  
  58.    
  59.  
  60.     public void showStatusBarNotification(int notificationID, String tickerText, String contentTitle, String contentText, String url){
  61.  
  62.  
  63.  
  64.                 String ns = Context.NOTIFICATION_SERVICE;
  65.  
  66.                 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  67.  
  68.                 int icon = R.drawable.f2f_icon;        
  69.  
  70.                 long when = System.currentTimeMillis();
  71.  
  72.  
  73.  
  74.                 Notification notification = new Notification(icon, tickerText, when);
  75.  
  76.                 notification.defaults |= Notification.DEFAULT_SOUND;
  77.  
  78.                 notification.defaults |= Notification.DEFAULT_VIBRATE;
  79.  
  80.                 notification.flags |= Notification.FLAG_AUTO_CANCEL;
  81.  
  82.  
  83.  
  84.                
  85.  
  86.                 Context context = getApplicationContext();             
  87.  
  88.                                
  89.  
  90.                 Intent notificationIntent = new Intent(this, NotificationActivity.class);
  91.  
  92.                 notificationIntent.putExtra("page", url);
  93.  
  94.                
  95.  
  96.                  
  97.  
  98.                 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
  99.  
  100.                 notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);            
  101.  
  102.                
  103.  
  104.                 mNotificationManager.notify(notificationID, notification);
  105.  
  106.         }
  107.  
  108. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4




Thanks for your help!
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Top

Postby pskink » Fri Mar 26, 2010 8:35 am

i think that SharedPreferences would do the trick

in onCreate get sharedPreferences and boolean first = getBoolean("first", true)

if first == true it means that Activity started because you pressed notification and then you can display intent data and then call edit() on sharedPreferences and putBoolean("first", false) on Editor and then commit()
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby pskink » Fri Mar 26, 2010 8:44 am

one more question: what is onCreate's Bundle savedInstanceState parameter in both cases?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Fri Mar 26, 2010 10:11 am

After displaying intent data, if I call edit() on sharedPreferences and set putBoolean("first", false). After few seconds activity can send another notification and then again I will have to set putBoolean("first", true). Now, if user minimize and restores the app, still the intent be fired.

I don't know much about Bundle savedInstanceState parameter, but as per docs, it helps to save instance state by overriding onSaveInstanceState() method.

BTW, I have tried different apps, but all are facing the same problem when app restores.
Also, I want to send multiple notifications such that there should be one icon on status bar, but show multiple notifications in the expended view of status bar. I have seen different applications doing that, but I am not able to do this.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Fri Mar 26, 2010 10:19 am

araja wrote:I don't know much about Bundle savedInstanceState parameter, but as per docs, it helps to save instance state by overriding onSaveInstanceState() method.


but what are values in both cases?

null, not null?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Fri Mar 26, 2010 10:25 am

null in both cases
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Top

Postby pskink » Fri Mar 26, 2010 10:41 am

araja wrote:null in both cases


so onSaveInstanceState was not called?

then i give up, i dont have any other ideas...
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Fri Mar 26, 2010 11:09 am

Any idea about my second question? I am pasting it here:

I want to send multiple notifications such that there should be one icon on status bar, but show multiple notifications in the expended view of status bar. I have seen different applications doing that, but I am not able to do this.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby pskink » Fri Mar 26, 2010 11:18 am

araja wrote:Any idea about my second question? I am pasting it here:

I want to send multiple notifications such that there should be one icon on status bar, but show multiple notifications in the expended view of status bar. I have seen different applications doing that, but I am not able to do this.


sorry, never seen this - probably apps are doing it on their own

but i have (i hope) answer for first question

in onCreate:

i = getIntent()
flags = i.getFlags()

test if flags has bit Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby araja » Fri Mar 26, 2010 12:24 pm

Great!
This is the correct solution of the problem.

Thanks
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Top
Previous

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 24 guests