Pergunta

My app is running a service that collects feeds. When it find these feeds it create notations (unsuccessfully). I use a method call like this:

doNotification(date,"New Article",title,link,content,description,false);

for articles and this:

doNotification(date,"New Video",title,link,"","",true);

for videos. The method is this:

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, 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, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
            notification.setLatestEventInfo(feedGetter.service.getApplicationContext(), title, subtext, contentIntent);
            mNotificationManager.cancel(video? 1 : 0);
            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
            }
        }
    }

As you see I add some data to the intent so I can handle the notifications correctly. The notifications are assigned to an ID for videos or an ID for articles and should replace the previous notifications. Here is the NotificationActivity that handles the notifications:

public class NotificationActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Debug.out("Notification Activity");
        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(1);
        }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(0);
        }
        finish();
    }
}

So it's supposed to activate a URL for the videos and restart the application for articles with some data for handling the articles so the article is displayed to the user.

Seems simple enough but it doesn't work. The notifications display and they replace each other on the notification menu, showing the latest notifications for videos and articles but when I click on them they go wrong. I try to click on the article notification and it thinks it is a video and loads one of the videos. I go back onto the notification menu and the video notification has disappeared even when I clicked on the article notification. I try clicking on the article notification and nothing happens. It literally closes the menu and doesn't nothing and the notification remains in the menu doing nothing.

Thank you for any help with this. I am targeting the Google APIs level 14 API, with a min SDK version of level 8, trying with a 2.2.1 Android tablet.

Foi útil?

Solução

The problem is with the Pending intent. Even though the docs say the requestCode is not used, it is. You must pass a unique integer for each PendingIntent. That worked!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top