问题描述
首先,这不是重复的,因为我已经实施了那些根本不起作用的解决方案。
我正在尝试以每天的间隔创建多个重复的通知警报。但是。
所以,首先在我的 MainActivity.java 中,我有这段代码可以在 OnDestroy() 中安排警报,稍后我将对其进行更改:
@Override
protected void onDestroy() {
//1st notification
Calendar calendarnoti = Calendar.getInstance();
calendarnoti.set(Calendar.HOUR_OF_DAY,19);
calendarnoti.set(Calendar.MINUTE,29);
calendarnoti.set(Calendar.SECOND,55);
Intent intent = new Intent(MainActivity.this,Notification_receiver.class);
PendingIntent pendingIntent = PendingIntent.getbroadcast(MainActivity.this,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendarnoti.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
//2nd notification
Calendar calendarnoti1 = Calendar.getInstance();
calendarnoti1.set(Calendar.HOUR_OF_DAY,8);
calendarnoti1.set(Calendar.MINUTE,59);
calendarnoti1.set(Calendar.SECOND,55);
Intent intent1 = new Intent(MainActivity.this,Notification_receiver.class);
PendingIntent pendingIntent1 = PendingIntent.getbroadcast(MainActivity.this,99,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager1 = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendarnoti1.getTimeInMillis(),pendingIntent1);
super.onDestroy();
}
这是我的广播接收器代码:
age com.techystudios.homework.NotifybroadcastReceiver;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ringtoneManager;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.techystudios.homework.MainActivity;
import com.techystudios.homework.R;
import com.techystudios.homework.data.MyDBHandler;
import com.techystudios.homework.model.Points;
import java.util.Calendar;
import static android.content.Context.MODE_PRIVATE;
public class Notification_receiver extends broadcastReceiver {
int sYear;
int sMonth;
int sDate;
@Override
public void onReceive(Context context,Intent intent) {
//1st notification
MyDBHandler db = new MyDBHandler(context);
Calendar currentcal = Calendar.getInstance();
int Year = currentcal.get(Calendar.YEAR);
int Month = currentcal.get(Calendar.MONTH) + 1;
int dayofmonth = currentcal.get(Calendar.DAY_OF_MONTH);
db.close();
String notdone = Integer.toString(db.getnotDoneWorks(Year,Month,dayofmonth));
notificationmanager notificationmanager = (notificationmanager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context,MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);
if (notdone.equals("0")) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"techystudioschannelUnIquEID")
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
.setContentTitle("Todo Homework")
.setContentText("You don't have any homework dues today.")
.setStyle(new NotificationCompat.BigTextStyle().bigText("You don't have any homework dues today."))
.setSound(alarmSound)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000,1000,1000});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "techystudioschannelUnIquEID";
NotificationChannel channel = new NotificationChannel(
channelId,"Remind me of remaining homeworks before the day ends",notificationmanager.IMPORTANCE_HIGH);
notificationmanager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
notificationmanager.notify(0,builder.build());
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,R.mipmap.ic_launcher))
.setContentTitle("Todo Homework")
.setContentText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends.")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends."))
.setSound(alarmSound)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000,1000});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "techystudioschannelUnIquEID";
NotificationChannel channel = new NotificationChannel(
channelId,builder.build());
}
Calendar calendar = Calendar.getInstance();
SharedPreferences getshrd = context.getSharedPreferences("dtValues",MODE_PRIVATE);
sYear = getshrd.getInt("sYear",0);
sMonth = getshrd.getInt("sMear",0);
sDate = getshrd.getInt("sDate",0);
Points points = new Points(context,sYear,sMonth,sDate,calendar);
MyDBHandler db1 = new MyDBHandler(context);
String todoWork = Integer.toString(db1.getCount());
db1.close();
// notificationmanager notificationmanager1 = (notificationmanager) context
// .getSystemService(Context.NOTIFICATION_SERVICE);
// Intent notificationIntent1 = new Intent(context,MainActivity.class);
// notificationIntent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context,PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound1 = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);
if (!todoWork.equals("0")) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"techystudioschannelUnIquEID1")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Todo Homework")
.setContentText("Good Morning! You have total " + todoWork + " homeworks to complete.")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have total " + todoWork + " homeworks to complete."))
.setSound(alarmSound1)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent1)
.setVibrate(new long[]{1000,1000});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "techystudioschannelUnIquEID1";
NotificationChannel channel = new NotificationChannel(
channelId,"Remind me for completing my homeworks",builder.build());
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"techystudioschannelUnIquEID1")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Todo Homework")
.setContentText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax.")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax."))
.setSound(alarmSound1)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent1)
.setVibrate(new long[]{1000,builder.build());
}
}
}
好的,现在我做了什么,结果如何:
-
再次尝试使用不同的请求代码,结果与上述相同。
-
然后我也尝试为通知管理器和所有这些东西保留相同的对象,而不是创建一个新的对象,然后结果与第三个指针的情况相同。您可以看到注释掉的第二个通知的新对象。
-
最后,我尝试使用“intent”对象从 MainActivity 发送 Intent Extras,并在接收器中放置了一个 if/else 块,但在预定的时间再次不会触发任何通知。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)