如何解决Android:设置特定时间的通知
我正试图在第二天早上7点收到通知以提醒我,我以为我已经进行了一次此操作,但一定做了一些更改,但没有意识到。谁能告诉我我要怎么做?我已经包括设置提醒和通知发布者的活动。
活动
public class Reminders extends MainActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
final static int req1 = 1;
private final static String default_notification_channel_id = "default";
private static final int RQS_PICK_CONTACT = 0;
public String a = "0";
EditText eText;
EditText reminder;
DatePickerDialog picker;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setonNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
Intent a = new Intent(Reminders.this,MainActivity.class);
startActivity(a);
break;
case R.id.recipes:
Intent b = new Intent(Reminders.this,RecipeSearch.class);
startActivity(b);
break;
case R.id.shoppingList:
Intent c = new Intent(Reminders.this,ShoppingList.class);
startActivity(c);
break;
case R.id.mealPlan:
Intent d = new Intent(Reminders.this,MenuPlan.class);
startActivity(d);
break;
case R.id.reminder:
Intent e = new Intent(Reminders.this,Reminders.class);
startActivity(e);
break;
}
return false;
}
});
reminder = findViewById(R.id.reminderText);
eText = findViewById(R.id.reminderDate);
eText.setInputType(InputType.TYPE_NULL);
eText.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
final int day = cldr.get(Calendar.DAY_OF_MONTH);
final int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
// date picker dialog
picker = new DatePickerDialog(Reminders.this,new DatePickerDialog.OnDateSetListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
eText.setText(year + "/" + (monthOfYear + 1) + "/" + dayOfMonth);
}
},year,month,day);
picker.show();
}
});
}
private void scheduleNotification(Notification notification,long delay) {
Intent notificationIntent = new Intent(Reminders.this,NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION,notification);
PendingIntent pendingIntent = PendingIntent.getbroadcast(this,notificationIntent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,delay,pendingIntent);
}
private Notification getNotification(String content) {
String description = reminder.getText().toString();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
builder.setContentTitle("Reminder Service");
builder.setContentText(description);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setStyle(new NotificationCompat.BigTextStyle());
builder.setVibrate(new long[]{1000,1000})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLights(Color.RED,500,500);
builder.setAutoCancel(true);
builder.setPriority(PRIORITY_HIGH);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
Context context;
PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);
builder.setContentIntent(pendingIntent);
return builder.build();
}
public void saveReminder(View view) {
String myFormat = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat,Locale.getDefault());
String date = eText.getText().toString();
Date date1 = null;
long milli = 0;
try {
date1 = sdf.parse(date);
milli = date1.getTime();
} catch (ParseException e) {
e.printstacktrace();
}
Log.d("date",date);
// Set date at 7AM
long delay = (milli + 25200000);
scheduleNotification(getNotification(reminder.getText().toString()),delay);
Intent intentHome = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentHome);
}
}
NotificationPublisher
public class NotificationPublisher 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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。