我正在尝试在自定义日期添加通知。我没有收到任何错误,但没有出现通知。请给我建议你的答案,我如何获得有关datetimepicker的通知。如果还有另一种在自定义datetimepicker中添加通知的方法,也请提出建议。预先谢谢你
我的AddeditTaskActivity类
private void scheduleNotification(Notification notification, long delay) {
Intent notificationIntent = new Intent(this, MyNotificationPublisher.class);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delay, pendingIntent);
}
private Notification getNotification(String content) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, default_notification_channel_id);
builder.setContentTitle("Check Your TODO");
builder.setContentText(content);
builder.setSmallIcon(R.drawable.ic_launcher_foreground);
builder.setAutoCancel(true);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
return builder.build();
}
private void showDateTimeDialog() {
final Calendar calendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, final int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("EEEE, dd MMM yyyy" +" "+ "hh:mm"+" "+"a");
Date date = calendar.getTime();
tvDate.setText(simpleDateFormat.format(date));
scheduleNotification(getNotification(tvDate.getText().toString()), date.getTime());
}
};
new TimePickerDialog(AddEditTaskActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(AddEditTaskActivity.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
}
***MyNotification Publisher Class***
public class MyNotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id" ;
public static String NOTIFICATION = "notification" ;
public void onReceive (Context context , Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
int importance = NotificationManager. IMPORTANCE_HIGH ;
NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel) ;
}
int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
assert notificationManager != null;
notificationManager.notify(id , notification) ;
}
}
***Android Manifest***
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.todosaman">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity.SplashScreen"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity.ActivityLogin"/>
<activity android:name=".Activity.RegisterActivity"/>
<activity android:name=".Activity.AddEditTaskActivity"
android:parentActivityName=".Activity.MainActivity"></activity>
<activity android:name=".Activity.MainActivity">
</activity>
<receiver android:name=".Activity.MyNotificationPublisher" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>