4

プッシュ通知アプリがあります。プッシュ通知が来ると、BroadcastReceiver は GCMIntentService を呼び出して通知を設定します

mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, HomeActivity.class);
        Intent intent = new Intent(this, AddToCalendarIntentService.class);
        intent.putExtra(JSON_KEY_CLASS_ID, classid);
        intent.putExtra(JSON_KEY_CLASS_NAME, classname);
        intent.putExtra(Config.JSON_KEY_DATE, tgl);
        intent.putExtra(Config.JSON_KEY_TIME_START, timestart);
        intent.putExtra(Config.JSON_KEY_TIME_END, timeend);
        intent.putExtra(Config.JSON_KEY_VENUE, venue);
        Log.d(LOG_TAG, classid + ", " + classname + ", " + venue);
        PendingIntent contentIntent = PendingIntent.getService(this, 0,
                intent, 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_notif)
                        .setContentTitle("Re: " + classname)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(pesan))
                        .setContentText(pesan)
                        .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                        .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .addAction(R.drawable.ic_action_add_person, "Add to Calendar", contentIntent)
                        .setAutoCancel(true)
                        .setTicker("Reschedule Class");

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

ユーザーが [カレンダーに追加] をクリックすると、PendingIntent が呼び出され、すべてのパラメーターで AddToCalendarIntentService が開始されます。

public class AddToCalendarIntentService extends IntentService {

public AddToCalendarIntentService() {
    super("AddToCalendarIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    final String JSON_KEY_CLASS_NAME = "classname";
    final String JSON_KEY_CLASS_ID = "classid";
    Bundle extra = intent.getExtras();
    String title = extra.getString(JSON_KEY_CLASS_NAME);
    String location = extra.getString(Config.JSON_KEY_VENUE);
    String tgl = extra.getString(Config.JSON_KEY_DATE);
    String[] tglSplit = tgl.split("-");
    int year = Integer.parseInt(tglSplit[0]);
    int month = Integer.parseInt(tglSplit[1]);
    int date = Integer.parseInt(tglSplit[2]);

    String timestart = extra.getString(Config.JSON_KEY_TIME_START);
    String timeend = extra.getString(Config.JSON_KEY_TIME_END);
    String[] start = timestart.split(":");
    String[] end = timeend.split(":");
    int hourStart = Integer.parseInt(start[0]);
    int minStart = Integer.parseInt(start[1]);
    int hourEnd = Integer.parseInt(end[0]);
    int minEnd = Integer.parseInt(end[1]);
    Log.d("INTENT SERVICE", location);
    TaskHelper.addToCalendar(this, "Reschedule: " + title, location, year, month-1, date, hourStart, minStart, hourEnd, minEnd);
    NotificationManager mNotification = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotification.cancel(1);
}
}

コードは機能しますが、別の通知を再度受け取ってカレンダーに追加すると、AddToCalendarIntentService のパラメーターは古いパラメーターを保持し、新しいパラメーターは無視されます。たとえば、最初の場所はIndonesiaで、 GCMIntentServiceAddToCalendarIntentServiceの両方のログにIndonesiaが表示されます。

2 回目の通知では、場所はSingaporeであり、 GCMIntentServiceでは正しく表示されますが、 AddToCalendarIntentServiceにログインすると、インドネシアと表示されます (シンガポールのはずです)。

助けてください。

ありがとうございました。

4

2 に答える 2

3

これは、システムが同じ PendingIntent と見なすためです。いくつかの保留中のインテントが必要な場合は、それらを区別する必要があります。

最も簡単な方法は、異なる requestCodes (PendingIntent.getService の 2 番目のパラメーター) を指定することです。

詳細については、こちらをご覧ください。

http://developer.android.com/reference/android/app/PendingIntent.html

于 2014-11-01T12:36:06.307 に答える