【问题标题】:Getting Same data on all notifications在所有通知上获取相同的数据
【发布时间】:2016-03-10 14:53:52
【问题描述】:

我正在创建一个事件,我想为事件创建通知。为此,我使用了警报管理器和广播接收器。

我已经传递了用于调用广播接收器的意图的事件数据,但我为每个通知获取相同的数据。

这里出了什么问题?

设置闹钟

public void setNotificationTime(Calendar c)
{

    Date dateFrom = new Date();
    df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
    try {
        dateFrom = df.parse(startTime);
    }
    catch (ParseException ex) {

    }

    dateFrom.getTime();
    c.setTime(dateFrom);

    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    if(notificationTime !=null  &&  !notificationTime.isEmpty()) {


        if (notificationTime.equals("10 Minutes Before")) {

            FLAG = 1;

            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute - 10);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

           // setAlarm(c, FLAG);
            Intent intent = new Intent(getBaseContext(), NotificationReceiver.class);

            intent.putExtra("startTime",startTime);
            intent.putExtra("endTime",endTime);
            intent.putExtra("location",location);
            intent.putExtra("title",eventTitle);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0 , intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();


        }
}

通知接收者:

public class NotificationReceiver  extends BroadcastReceiver {


public static int MY_NOTIFICATION_ID = 0;
private NotificationManager notificationManager;
private Notification myNotification;
private String location,title,fromDate,toDate;


@Override
public void onReceive(Context context, Intent intent) {

    Random rand = new Random();
    int id = rand.nextInt(1000000) + 1;

    Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();


        Calendar c = Calendar.getInstance();
        Date date = new Date();
        Date date1 = new Date();

        SimpleDateFormat df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
        SimpleDateFormat df2 = new SimpleDateFormat("hh:mm a");

    fromDate =  intent.getStringExtra("startTime");
    toDate = intent.getStringExtra("endTime");
    location = intent.getStringExtra("location");
    title = intent.getStringExtra("title");


    try {
        date = df.parse(fromDate);
        date1 = df.parse(toDate);
    } catch (ParseException ex) {

    }


    String timeFrom = df2.format(date);
    //   String startTime = String.valueOf(timeFrom);

    String timeTo = df2.format(date1);
    // String endTime = String.valueOf(timeTo);



    Intent myIntent = new Intent(context, MainActivity.class);


        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if(location.equals(""))
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo;

            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        else
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo + "\nAt : " + location;
            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        Log.i("Notify", "Notification");
        notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, myNotification);

        myNotification.flags=Notification.FLAG_AUTO_CANCEL;

        MY_NOTIFICATION_ID++;


    }

}

谢谢。

【问题讨论】:

    标签: android apple-push-notifications alarmmanager android-broadcastreceiver


    【解决方案1】:

    我认为您正在重用相同的PendingIntent,因为您正在使用类似的Intent 创建它。鉴于您的意图之间的唯一区别是额外的数据, Android 将它们归类为相同的intents,因此不会创建新的PendingIntent,而是重用现有的PendingIntent。更多信息here

    在创建PendingIntent 时尝试使用FLAG_CANCEL_CURRENT 或确保Intent.filterEquals 考虑的Intent 属性对于每个新PendingIntent 都不同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      相关资源
      最近更新 更多