Почему startForeground не работает?Уведомления - PullRequest
0 голосов
/ 11 декабря 2018

У меня есть код для 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" />

Как это исправить?

...