简体   繁体   English

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

[英]Notification Intents not working for Android application

My app is running a service that collects feeds. 我的应用正在运行收集Feed的服务。 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. 如您所见,我向intent添加了一些数据,因此我可以正确处理通知。 The notifications are assigned to an ID for videos or an ID for articles and should replace the previous notifications. 通知将分配给视频的ID或文章的ID,并应替换以前的通知。 Here is the NotificationActivity that handles the notifications: 以下是处理通知的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();
    }
}

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. 因此,它应该激活视频的URL并重新启动应用程序以查找包含用于处理文章的一些数据的文章,以便向用户显示文章。

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. 我的定位是Google API 14级API,最低版本为8级,尝试使用2.2.1 Android平板电脑。

The problem is with the Pending intent. 问题在于待定意图。 Even though the docs say the requestCode is not used, it is. 即使文档说不使用requestCode,它也是。 You must pass a unique integer for each PendingIntent. 您必须为每个PendingIntent传递一个唯一的整数。 That worked! 那很有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM