[英]How to repeat android notifications after some Interval of hours
I am stuck with my app which needs to repeat one notification after one hour (Medical purpose).我坚持使用我的应用程序,它需要在一小时后重复一个通知(医疗目的)。 As my field is totally opposite and I am noob in coding any help will be appreciated.由于我的领域完全相反,我在编码方面是菜鸟,任何帮助将不胜感激。 I know I have to add something in notification receiver to repeat notifications.我知道我必须在通知接收器中添加一些东西来重复通知。 But every time I try to repeat the app crashes.但是每次我尝试重复应用程序时都会崩溃。
(This is a little but unique idea to solve a real world problem I will credit everyone from whom I've received even a little help ) (这是解决现实世界问题的一个小而独特的想法,我会感谢我从他们那里得到一点帮助的每个人)
Here is my MainActivity这是我的主要活动
private NotificationManagerCompat manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = NotificationManagerCompat.from(this);
}
public void Shownotification(View v) {
String title = "You Did it!";
String message = "Some Text";
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
manager.notify(0,notification);
}
Notification Channel通知渠道
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationChannel channel2 = new NotificationChannel(
CHANNEL_2_ID,
"Channel 2",
NotificationManager.IMPORTANCE_LOW
);
channel2.setDescription("This is Channel 2");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
Notification Reciever通知接收器
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String title = "You Did it!";
String message = "Some Text";
Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
Intent i = new Intent("android.action.DISPLAY_NOTIFICATION");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
}
您可以在上一个触发时设置新的通知。在 BroadcastReceiver 的 onReceive 方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.