获取所有通知的相同数据

时间:2016-03-10 14:53:53

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

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

我已经通过了用于调用广播接收器的意图的事件数据,但是每个通知都获得了相同的数据。

这里出了什么问题?

设置闹钟

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++;


    }

}

谢谢。

1 个答案:

答案 0 :(得分:0)

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

在创建PendingIntent时尝试使用Intent或确保Intent.filterEquals考虑的PendingIntent属性对于每个新{{1}}

都不同