我想在通知时显示特定事件的数据。喜欢事件标题,开始时间,结束时间和位置。
我已创建通知,但我不知道如何将此数据发送到通知中。
我在getAllEvents
类中创建了EventTableHelper
方法并从中检索数据。
这是我尝试创建通知并获取数据的方式。如何在通知中添加此数据?如何自定义此通知?
public class NotificationReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;
EventTableHelper db;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();
db = new EventTableHelper(context);
List<EventData> testSavings = db.getAllEvents();
for (EventData ts : testSavings) {
String log = "from date:" + ts.getFromDate()
+ " ,to date: " + ts.getToDate()
+ " ,location: " + ts.getLocation()
+ " ,title " + ts.getTitle();
Date date = new Date();
Date date1 = new Date();
Log.d("Result: ", log);
SimpleDateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
SimpleDateFormat df2 = new SimpleDateFormat("HH:mm a");
try {
date = df.parse(ts.getFromDate());
date1 = df.parse(ts.getToDate());
} catch (ParseException ex) {
}
String timeFrom = df2.format(date);
String startTime = String.valueOf(date);
String timeTo = df2.format(date1);
String endTime = String.valueOf(date1);
String location = ts.getLocation();
String title = ts.getTitle();
Intent myIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Event")
.setContentText("Time")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_alarm_black_48dp)
.build();
Log.i("Notify", "Notification");
notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
}
谢谢..