gpt4 book ai didi

java - 通知 Intent 不适用于 Android 应用程序

转载 作者:行者123 更新时间:2023-12-02 07:57:07 26 4
gpt4 key购买 nike

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

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

如您所见,我向 Intent 添加了一些数据,以便我可以正确处理通知。通知会分配给视频 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 平板电脑。

最佳答案

问题在于待处理的 Intent 。尽管文档说没有使用 requestCode,但确实如此。您必须为每个 PendingIntent 传递一个唯一的整数。成功了!

关于java - 通知 Intent 不适用于 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com