android - 如何根据日期发送通知
问题描述
我有一个日历应用程序,其中的信息是在线下载的,并且有一个事件的 recyclerView,我想在今天有日历事件时通知用户,并用事件名称或名称通知。我做了一些谷歌搜索,但无法弄清楚这一点。
在我的 MainActivity.java 中,这是我从在线页面创建的测试方法
private void addNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_check);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
但是当我调用这个方法时什么都没有发生,我做错了什么?
解决方案
我认为您缺少通知的渠道
我有不同的解决方案,试试这个(我在手机上检查过)
调用函数:
showSmallNotification(R.drawable.ic_launcher_background, "title","message");
功能码
private void showSmallNotification( int icon, String title, String message){
String CHANNEL_ID = "Channel_01";
String CHANNEL_NAME = "Notification";
// I removed one of the semi-colons in the next line of code
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
//channel.canShowBadge();
// Did you mean to set the property to enable Show Badge?
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setContentText(message);
// Don't forget to set the ChannelID!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationBuilder.setChannelId(CHANNEL_ID);
}
notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
}
编辑
为了检查每天是否有任何事件,您需要使用 AlarmManager,因为它的名称可以每天安排一个事件一旦您这样做了,您就可以看到一个解决方案(或者至少它会给出一个方向)我认为下一步是将意图发送到服务,该服务将检查当天是否有可用的事件。如果有任何只是显示通知/秒
推荐阅读
- sass - 覆盖多个 scss 文件中的变量
- java - 如何在 RxJava 中正确转换多播 observables
- ios - AVAudioPlayer 导致应用程序崩溃
- java - Apache Ignite 和 Yardstick
- java - 循环外的 Java 对象引用
- json - Haskell (Aeson) : 如何从 JSON 构造带有附加值的记录
- node.js - 反应部署错误 = 断言 `args[1]->IsString()'
- ruby-on-rails - 继承外键rails
- c - 如何计算这个递归函数的时间复杂度?(英国夏令时)
- javascript - 如何在 django 中使用 ajax 从 javascript 调用 python 函数