Notifications using wrong intents.

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

Notifications using wrong intents.

Postby MatthewM64 » Wed Feb 29, 2012 12:33 am

Hello there!

I have an app that receives article and video feeds from the internet periodically with a Service. It works OK except the notifications when the feeds are received. There are two notification IDs. 0 for videos and 1 for articles. The feeds are supposed to update/replace each notification for the latest notification received. I call a method doNotification like this:

Code: Select all
doNotification(date,"New Article",title,link,content,description,false);


Or for videos:

Code: Select all
doNotification(date,"New Video",title,link,"","",true);


The method looks like this:

Code: Select all
public void doNotification(Date date,String title,String subtext,String url,String body,String dateString,boolean video){
      long time = date.getTime();
      if(time > feedGetter.lastFeed){
         //New feed, do notification
         NotificationManager mNotificationManager = (NotificationManager) feedGetter.service.getSystemService(Context.NOTIFICATION_SERVICE);
         int icon = R.drawable.notification_icon;
         Notification notification = new Notification(icon, title + ": " + subtext, time);
         Intent notificationIntent = new Intent(feedGetter.service.getApplicationContext(), NotificationActivity.class);
         notificationIntent.putExtra("url",url);
         notificationIntent.putExtra("video",video);
         if(!video){
            notificationIntent.putExtra("body",body);
            notificationIntent.putExtra("date",dateString);
            notificationIntent.putExtra("title",subtext);
         }
         notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         PendingIntent contentIntent = PendingIntent.getActivity(feedGetter.service.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
         notification.setLatestEventInfo(feedGetter.service.getApplicationContext(), title, subtext, contentIntent);
         mNotificationManager.notify(video? 1 : 0, notification);
         //Update new time if necessary.
         if(time > feedGetter.newTime){
            feedGetter.newTime = time; //New time will be the time for this feed as it is the latest so far
         }
      }
   }


However when I click the latest notification it gives the wrong intent to NotificationActivity. It gives a previous intent. When I click on the article notification it gives one of the video intents for some bissare reason. Here is the NotificationActivity class as well:
Code: Select all
public class NotificationActivity extends Activity{
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if(getIntent().getBooleanExtra("video", true)){
           //Handle video notification
           Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("url")));
           startActivity(browserIntent);
           mNotificationManager.cancel(0);
        }else{
           //Start application UI and move to article
            Intent intent = new Intent(this,TheLibertyPortalActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("url", getIntent().getStringExtra("url"));
            intent.putExtra("body", getIntent().getStringExtra("body"));
            intent.putExtra("date", getIntent().getStringExtra("date"));
            intent.putExtra("title", getIntent().getStringExtra("title"));
          startActivity(intent);
          mNotificationManager.cancel(1);
        }
        finish();
   }
}


What am I doing wrong when creating these notifications?

Thanks for any answer!
MatthewM64
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 29, 2012 12:25 am

Top

Re: Notifications using wrong intents.

Postby MatthewM64 » Wed Feb 29, 2012 2:29 am

Look here: http://developer.android.com/reference/ ... nt.Context, int, android.content.Intent, int)

"requestCode Private request code for the sender (currently not used)."

This is wrong. I found that my problem is that I needed a unique requestCode for each PendingIntent and then my notifications used their own PendingIntents. Solved!
MatthewM64
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 29, 2012 12:25 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 27 guests