奇怪的通知行为

时间:2014-05-16 00:03:14

标签: android notifications

我有一个简单的应用程序(自定义日历)。每次用户创建事件时,都会创建一个带有自定义广播接收器的新AlarmManager,以便在事件开始前5分钟内调用通知。当那个时间到来并且用户点击通知时,一个简单的活动就会显示事件信息。

问题:当我运行应用并为第一次创建事件时,一切正常。当用户点击通知时,会显示正确的。但是,当我创建第二个事件时,会出现问题。当显示第二个通知的通知时,它看起来很正常(通知栏中显示正确的信息),但是当我点击它时,会显示第一个创建的事件。与第三,第四......相同......

如何创建警报管理器,它位于创建事件的活动中......

if (hasAlarm) {
                Bundle bundle = new Bundle();
                bundle.putString("name", eventName);
                bundle.putLong("time", dateStart);
                bundle.putString("desc", eventDescription);
                bundle.putString("location", eventLocation);
                bundle.putLong("end", dateEnd);
                bundle.putString("username", username);
                bundle.putString("password", password);
                bundle.putString("mailServer", mailServer);
                Intent alarmIntent = new Intent(this, AlarmReceiver.class);
                alarmIntent.putExtras(bundle);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                        0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, dateStart
                        - (1000 * 60 * 5), pendingIntent);
}

和自定义广播接收器......

public class AlarmReceiver extends BroadcastReceiver {

    private static int id = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String desc = bundle.getString("desc");
        long time = bundle.getLong("time");
        String name = bundle.getString("name");

        for (String key : bundle.keySet()) {
            Log.d("Bundle ALARM REVEIVER", key + " = \"" + bundle.get(key)
                    + "\"");
        }

        long[] vibrate = { 0, 100, 200, 300 };
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.stat_notify_chat)
                .setContentTitle("Upcoming event: " + name)
                .setContentText(desc).setWhen(time).setVibrate(vibrate)
                .setSound(alarmSound);

        Intent viewEvent = new Intent(context, ViewEvent.class);
        viewEvent.putExtras(intent.getExtras());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                viewEvent, 0);
        builder.setContentIntent(contentIntent);

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(id++, builder.build());
    }
}

1 个答案:

答案 0 :(得分:0)

我发现问题出在哪里......正如我们在documentation中看到的那样,我们还需要为广播接收器添加PendingIntent.FLAG_UPDATE_CURRENT,因此完整的实现看起来像

        Intent viewEvent = new Intent(context, ViewEvent.class);
        viewEvent.putExtras(intent.getExtras());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                viewEvent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);