我正在制作考勤跟踪系统。 所以,我需要在课程开始之前和课程开始之后发出通知。
主要方法是我为数据库中的每个课程创建通知。
mainMethod() {
testAlarmA(s[0], s[2]);
testAlarmB(s[0], s[2]);
}
我已经为每个意图实现了setAction()方法,并且每个待处理意图也有不同的请求代码(我已在本网站的早期问题中阅读过这些代码)
private void testAlarmA (String code) {
Log.i("attendance", "class-start");
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_ATTENDANCE_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("type", "start");
notificationIntent.putExtra("courseCode", code);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 111, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (2 * 60 * 1000), broadcast);
}
private void testAlarmB (String code){
Log.i("attendance", "class-end");
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_ATTENDANCE_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("type", "end");
notificationIntent.putExtra("courseCode", code);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 2);
PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 222, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (2 * 60 * 1000), broadcast);
}
通过广播接收器类,我正在提取附加内容以了解要触发的通知,即类开始或结束。
但只有class start notif是receiver,类结束Log Entry才会显示。
@Override
public void onReceive(Context context, Intent intent) {
Log.i("time", "onReceive");
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager = mNotificationManager;
if (intent.getExtras().containsKey("type")) {
Log.i("time", "containsKey");
String type = intent.getExtras().getString("type");
if (type.equals("end")) {
Log.i("time", "end");
endNotif(context);
} else if (type.equals("start")) {
Log.i("time", "start");
startNotif(context);
}
}
}
我还有通知构建器的用户唯一ID。
private void startNotif (Context context) {
Intent notificationIntent = new Intent();
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("You Have " + courseCode + " at " + roomNo)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_library_books_white_24dp)
.setLights(0xff00ff00, 1000, 1000)
.setAutoCancel(true);
mNotificationManager.notify(1, mBuilder.build());
}
我的最终通知会有3个不同的操作按钮。
private void endNotif (Context context) {
Intent notificationIntent = new Intent(context, AttendanceActivity.class);
notificationIntent.putExtra("courseCode", courseCode);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent classAttended = new Intent(context, AttendanceNotificationServiceClass.class);
classAttended.setAction("action");
classAttended.putExtra("courseCode", courseCode);
classAttended.putExtra("class", "attended");
PendingIntent pClassAttended = PendingIntent.getService(context, 11, classAttended, PendingIntent.FLAG_UPDATE_CURRENT);
Intent classBunked = new Intent(context, AttendanceNotificationServiceClass.class);
classBunked.setAction("action");
classBunked.putExtra("courseCode", courseCode);
classBunked.putExtra("class", "bunked");
PendingIntent pClassBunked = PendingIntent.getService(context, 22, classBunked, PendingIntent.FLAG_UPDATE_CURRENT);
Intent classOff = new Intent(context, AttendanceNotificationServiceClass.class);
classOff.setAction("action");
classOff.putExtra("courseCode", courseCode);
classOff.putExtra("class", "off");
PendingIntent pClassOff = PendingIntent.getService(context, 33, classOff, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_library_books_white_24dp)
.setContentTitle(courseCode + " Just Ended")
.setContentText("Did you attend " + courseCode + " ?")
.addAction(R.drawable.ic_thumb_up_white_24dp, "Attended", pClassAttended)
.addAction(R.drawable.ic_thumb_down_white_24dp, "Bucked", pClassBunked)
.addAction(R.drawable.ic_clear_white_24dp, "Class Off", pClassOff)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_HIGH)
.setLights(0xff00ff00, 1000, 1000)
.setAutoCancel(true);
mNotificationManager.notify(0, mBuilder.build());
}
但是当我启动应用程序时,1分钟后才会显示课程开始通知。 class-end notif应该在课程开始后1分钟显示。
开始显示应该是,即每分钟后。 但是最终的通知从未显示过,甚至没有显示过一次。
如果在方法testAlarmA()和testAlarmB()中,我进行了更改,使testAlarmB()在testAlarmA()之前触发,只显示B / end。 A / start将永远不会显示。
我的日志
07-14 22:04:49.258 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:04:49.286 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:04:49.287 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:06:29.165 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:06:29.173 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:06:29.173 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:07:59.689 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:07:59.690 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:07:59.690 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:09:52.756 30137-30143/in.ac.iitd.bsw.iitdapp W/art: Suspending all threads took: 5.590ms
07-14 22:10:29.623 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:10:29.624 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:10:29.624 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:13:23.618 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:13:23.618 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:13:23.657 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start
答案 0 :(得分:0)
在testAlarmA和testAlarmB方法中,您正在使用设置操作的Intent构造函数,不需要再次调用setAction。在setAction方法中,您传递Long.toString(System.currentTimeMillis()),我认为您错误地插入了它,尝试删除它。
看到这个意图构造函数: https://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String)
和setAction方法: https://developer.android.com/reference/android/content/Intent.html#setAction(java.lang.String)
答案 1 :(得分:0)
嗨创建pendingIntent对象时,请确保每个intent具有不同的通知ID,如下所示
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), notificationId, deleteIntent, 0);
其中notificationId是打开应用程序时需要传递一个,启动后需要另外一个