我在Notifications
中有一个JobService
的代码:
public class MyJobService extends JobService {
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
@Override
public boolean onStartJob(JobParameters params) {
Intent medication = new Intent(this, Medication.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(notificationChannel);
}
// to diaplay notification in DND Mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setContentTitle(Medication.medication.get(i).getDrug())
.setContentText("Время приема " + Medication.medication.get(i).getTime())
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logonotification)
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(medication);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
startForeground(1000, notificationBuilder.build());
mNotificationManager.notify(1000, notificationBuilder.build());
Toast.makeText(this, "Запуск сервиса", Toast.LENGTH_SHORT).show();
}
但是第startForeground(1000, notificationBuilder.build())
行不起作用,我在通知栏中没有看到我的通知。如果我删除此行,则显示通知,但不是前台。
在清单中-<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
我该如何解决?