英文:
Android Studio: Notification not showing even though it is called
问题
以下是翻译好的内容:
所以我有一个应用程序,我希望它每天早上6点(午夜时分)向我发送通知,我在尝试了闹钟后使用WorkManager实现了这一功能。我目前有一个扩展Worker类的类,如下所示:
public class NotificationWorker extends Worker {
Context context;
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
this.context = context;
}
@NonNull
@Override
public Result doWork() {
Intent resultIntent = new Intent(context, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
Notification.Builder notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("应用标题")
.setContentText("一些文本...")
.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) HomePage.getHomePage().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification.build());
return Result.success();
}
}
它由位于我的Activity的onCreate()函数中的此代码片段调用:
WorkRequest notificationRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
WorkManager.getInstance(this).enqueue(notificationRequest);
doWork()方法被调用,但没有显示通知。如何解决这个问题将不胜感激。
- 此代码“应该”仅在加载Activity时显示通知,有关如何使其每天重复的任何想法吗?(最好也使用WorkManager)
英文:
So I have an app that I want to send me a notification every day at 6 am (midnight), I am working with the WorkManager for that after trying Alarms. I currently have a class extending Worker as follows:
public class NotificationWorker extends Worker {
Context context;
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
this.context = context;
}
@NonNull
@Override
public Result doWork() {
Intent resultIntent = new Intent(context, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
Notification.Builder notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("App Title")
.setContentText("Some Text...")
.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) HomePage.getHomePage().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification.build());
return Result.success();
}
}
it is called by this snippet of code that is inside my onCreate() function of my Activity
WorkRequest notificationRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
WorkManager.getInstance(this).enqueue(notificationRequest);
the doWork() method is called but no notification is shown. Any help would be apreciated.
- This code "should" only show the notification upon loading the Activity, any ideas on how I can make it repeat every day? (preferably with WorkManager too)
答案1
得分: 0
从 Android 26 开始,通知必须传递一个正确的通知通道。
> 注意:如果您的目标是 Android 8.0(API 级别 26),并且在没有指定通知通道的情况下发布通知,则通知不会显示,系统会记录错误。
有关信息,请参阅以下文档。
您可以使用以下代码构建通道,其中 CHANNEL_ID 是您选择的字符串:
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = ctx.getString(R.string.my_channel_name);
String description = ctx.getString(R.string.my_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// 注册通道
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
然后在初始化通知构建器时,传递额外的通道 ID 参数:
Notification.Builder notification = new Notification.Builder(context, CHANNEL_ID);
通常还会希望按以下方式为您的构建器传递优先级,以支持 Android 25 及更低版本:
notification.setPriority(NotificationCompat.PRIORITY_DEFAULT);
英文:
Starting in Android 26, Notifications must be passed a propper notification Channel.
> Caution: If you target Android 8.0 (API level 26) and post a
> notification without specifying a notification channel, the
> notification does not appear and the system logs an error.
Please see the following docs for information.
You can use the following code to build the channel, where CHANNEL_ID is a string of your choosing
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = ctx.getString(R.string.my_channel_name);
String description = ctx.getString(R.string.my_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
//Register Channel
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Then when initializing your Notification Builder, pass an additional parameter for the Channel ID
Notification.Builder notification = new Notification.Builder(context, CHANNEL_ID)
You will usually also want to pass a priority to your builder as follows, to support Android 25 and lower
notification.setPriority(NotificationCompat.PRIORITY_DEFAULT);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论