问题描述
我想从应用程序发送每日通知,我可以发送通知,但是当应用程序被终止/不在后台时,AlarmManager不会触发通知。
下面是我尝试过的代码,
private void createNotificationChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
CharSequence name = "Notification";
String description = "Channel for Notification";
int importance = notificationmanager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("Notification",name,importance);
channel.setDescription(description);
notificationmanager notificationmanager = getSystemService(notificationmanager.class);
notificationmanager.createNotificationChannel(channel);
}
}
private void setnotificationSendService()
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//creating a new intent specifying the broadcast receiver
Intent i = new Intent(this,Reminderbroadcast.class);
//creating a pending intent using the intent
PendingIntent pi = PendingIntent.getbroadcast(this,i,0);
//setting the repeating alarm that will be fired every day
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY,10);
alarmStartTime.set(Calendar.MINUTE,0);
alarmStartTime.set(Calendar.SECOND,0);
am.setRepeating(AlarmManager.RTC_WAKEUP,(alarmStartTime.getTimeInMillis()),AlarmManager.INTERVAL_DAY,pi);
}
我的接收者:
context.startService(new Intent(context,NotifyService.class));
我的服务
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),"Notification")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello")
.setContentText("Notification Test")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationmanagerCompat notificationmanager = notificationmanagerCompat.from(getApplicationContext());
notificationmanager.notify(200,builder.build());
有办法吗? 每天上午10点和晚上7点(或在任意1个特定时间)发送通知
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)