通知意图不适用于Android应用程序

时间:2012-02-28 01:28:31

标签: java android android-intent android-notifications

我的应用正在运行收集Feed的服务。当它找到这些提要时,它会创建符号(失败)。我使用这样的方法调用:

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

文章及其:

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

视频。方法是这样的:

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
            }
        }
    }

如您所见,我向意图添加了一些数据,因此我可以正确处理通知。通知将分配给视频的ID或文章的ID,并应替换以前的通知。以下是处理通知的NotificationActivity:

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();
    }
}

因此,应该激活视频的URL并重新启动应用程序以获取包含处理文章的一些数据的文章,以便向用户显示文章。

看起来很简单但不起作用。通知显示并在通知菜单上相互替换,显示视频和文章的最新通知,但是当我点击它们时它们会出错。我尝试点击文章通知,它认为这是一个视频并加载其中一个视频。我回到通知菜单,即使我点击文章通知,视频通知也已消失。我尝试点击文章通知,没有任何反应。它实际上关闭了菜单并且没有任何内容,通知仍然在菜单中无所事事。

感谢您对此提供任何帮助。我定位的是Google API 14级API,最低级别为8的SDK版本,尝试使用2.2.1 Android平板电脑。

1 个答案:

答案 0 :(得分:2)

问题在于待定意图。即使文档说不使用requestCode,它也是。您必须为每个PendingIntent传递一个唯一的整数。那很有效!