我有使用日历和日期选择器设置的每周通知。但是,我尝试设置的此通知使用的是数据库中的日期,该日期已转换为毫秒。包含毫秒的变量用作触发提醒的时间。但是,它将不起作用。该代码不会引发任何错误,根本不会触发通知。我的代码如下。
MainActivity-用于触发通知
// Variable pulled from mysql with the app date
private String firstAppointment;
// Date of first app converted from String to Date format
private Date dateOfFirstAppointment;
// Date converted into milliseconds for the notification.
private long appointmentInMilliseconds;
/**
* Method used to format the date from the MYSQL server
*/
private void getFirstAppointment() {
String myDate = firstAppointment;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
dateOfFirstAppointment = sdf.parse(myDate);
appointmentInMilliseconds = dateOfFirstAppointment.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* Method which sets a weekly notification
*/
private void appointmentReminder() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver2.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 5, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, appointmentInMilliseconds, pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, appointmentInMilliseconds, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
Toast.makeText(this, "Appointment Reminder Set", Toast.LENGTH_SHORT).show();
}
通知类别:
public class NotificationHelperChannel2 extends ContextWrapper {
public static final String channe2ID = "channe2ID";
public static final String channe2Name = "Channe2 Name";
private NotificationManager mManager;
public NotificationHelperChannel2(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(channe2ID, channe2Name, NotificationManager.IMPORTANCE_HIGH);
getManager2().createNotificationChannel(channel);
}
public NotificationManager getManager2() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannelNotification2() {
return new NotificationCompat.Builder(getApplicationContext(), channe2ID)
.setContentTitle("Appointment Reminder")
.setContentText("You have an appointment tomorrow")
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(icon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVibrate(new long []{1000, 1000})
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true);
}
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.brain_meditate_icon);
}
警报接收器类别:
public class AlertReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelperChannel2 notificationHelper2 = new NotificationHelperChannel2(context);
NotificationCompat.Builder nb2 = notificationHelper2.getChannelNotification2();
notificationHelper2.getManager2().notify(2, nb2.build());
}
}
答案 0 :(得分:0)
在不同的Android操作系统中,通知具有不同的处理方式。 可能由于您使用的是Pie,Oreo或至少需要特殊处理的Nougat Android,该警报无法正常工作
这是我在Oreo,Pie和牛轧糖中处理通知的代码。
可能会帮助您。
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("Branch Checklist")
.setContentText(resultValue)
.setVibrate(new long[]{100, 250})
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(resultValue));
// TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// stackBuilder.addNextIntent(intent);
// PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
// 0,
// PendingIntent.FLAG_UPDATE_CURRENT
// );
// mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());