android - 如何每天早上 7 点显示本地通知
问题描述
一切正常,通知运行正常。但是当通知必须显示时,我已经在 MainActivity 中设置了时间。但我不认为它工作正常。我在晚上 12 点收到通知 :( 我做错了什么。请帮忙。
我的通知助手代码,
public class NotificationHelper {
private Context mContext;
private static final String NOTIFICATION_CHANNEL_ID = "10001";
NotificationHelper(Context context) {
mContext = context;
}
void createNotification()
{
Bitmap bigImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.verse_img_2);
Bitmap thumbnail = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher);
Intent intent = new Intent(mContext , MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
100 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.drawable.ic_prayer_request);
mBuilder.setContentTitle("Verse of the Day!")
.setContentText("Click on the 'Notification' to read the verse!")
.setAutoCancel(false)
.setLargeIcon(thumbnail)
.setAutoCancel(true)
.setSound(null)
// .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigImage))
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Verse of the day!", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(100 /* Request Code */, mBuilder.build());
}
}
主要活动代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Methods Call
dailyVerseNotification();
}
private void dailyVerseNotification() {
Calendar time = Calendar.getInstance();
time.setTimeZone(TimeZone.getTimeZone("GMT"));
time.set(Calendar.HOUR_OF_DAY, 6);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.AM_PM, Calendar.AM);
time.set(Calendar.SECOND, 10);
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert alarmManager != null;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
解决方案
通过检查当前日期来修复。
Calendar calendar = Calendar.getInstance();
//Setting time of the day (7 AM here) Notification will be sent everyday.
Calendar currentDate = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
if(currentDate.after(calendar)){
Log.w("Added a day", "1");
calendar.add(Calendar.DATE, 1);
}
推荐阅读
- ajax - 使用ajax从json读取和显示值
- angular - 如何覆盖 ngrx 商店中的某些操作?
- sql - sql group 通过忽略大小写和后缀或最后一个字母
- r - 使用组变量创建新列
- unity3d - 从一个对象到另一个对象的光线投射
- c# - 从 C# 程序访问 C++ DLL 中的多个函数
- python - 在 Windows 上使用 Python 运行可执行文件并提供文本文件作为输入
- .net - 通过 YouTube V3 API 创建多个 YouTube 播放列表时出错?
- php - 循环遍历多维数组以预填充动态生成的表单
- python - 在两列中查找 1 和 0 组合的数量