0

我的应用程序正在运行一项收集提要的服务。当它找到这些提要时,它会创建符号(不成功)。我使用这样的方法调用:

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,最小 SDK 版本为 8 级,尝试使用 2.2.1 Android 平板电脑。

4

1 回答 1

2

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

于 2012-02-29T01:59:56.543 回答