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 をアクティブ化し、記事を処理するためのデータを使用して記事のアプリケーションを再起動し、記事がユーザーに表示されるようにする必要があります。

十分に単純に思えますが、うまくいきません。通知が表示され、通知メニューで互いに置き換えられ、ビデオと記事の最新の通知が表示されますが、それらをクリックするとうまくいきません。記事の通知をクリックしようとすると、それがビデオであると認識され、ビデオの 1 つが読み込まれます。通知メニューに戻り、記事通知をクリックしても動画通知が消えてしまいました。記事の通知をクリックしようとしましたが、何も起こりません。文字通りメニューを閉じて何もせず、通知は何もせずにメニューに残ります。

これについて何か助けてくれてありがとう。2.2.1 Android タブレットを使用して、レベル 8 の最小 SDK バージョンで Google API レベル 14 API をターゲットにしています。

4

1 に答える 1

2

問題は Pending インテントにあります。ドキュメントには requestCode が使用されていないと書かれていますが、使用されています。PendingIntent ごとに一意の整数を渡す必要があります。それはうまくいきました!

于 2012-02-29T01:59:56.543 に答える