Android 在所有通知上获取相同的数据
我正在创建一个事件,我想为事件创建通知。为此,我使用了报警管理器和广播接收器 我已经有意地传递了用于调用广播接收器的事件数据,但每次通知都会得到相同的数据 这里怎么了 设定报警Android 在所有通知上获取相同的数据,android,apple-push-notifications,alarmmanager,android-broadcastreceiver,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 { da
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将它们分类为相同的意图
,因此不再创建新的pendingent
,而是重用现有的pendingent。更多信息
创建pendingent
时,请尝试使用FLAG\u CANCEL\u CURRENT
或确保Intent考虑的Intent
属性。对于每个新的pendingent