Я работаю с уведомлением в приложении для Android
Уведомление работает отлично в любой версии под O.
В Oreo его иногда (Отображение / нажатие) работает, а иногда нет, Когда язакрыть приложение полностью его работу, а также если я заблокировал устройство ... но не более 5 минут после закрытия.
Я делаю все, что видел .. Я ставлю разрешения:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
и я работаю с JobIntentService
также я проверил SDK VERSION после уведомления о сборке ...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
также я установил канал уведомления:
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
но все, что ничего не меняет ...
Я не знаю, что мне делать.любая помощь, пожалуйста, и спасибо.
мой код:
NotificationEventReceiver.class
package com.example.android.homepharmacy.broadcast_receivers;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import com.example.android.homepharmacy.notifications.NotificationIntentService;
import java.util.Calendar;
import java.util.Date;
/**
* WakefulBroadcastReceiver used to receive intents fired from the AlarmManager for showing notifications
* and from the notification itself if it is deleted.
*/
public class NotificationEventReceiver extends WakefulBroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
public static void setupAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
getTriggerAt(new Date()),
60000 ,
alarmIntent);
}
public static void cancelAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.cancel(alarmIntent);
}
private static long getTriggerAt(Date now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
return calendar.getTimeInMillis();
}
private static PendingIntent getStartPendingIntent(Context context) {
Intent intent = new Intent(context, NotificationEventReceiver.class);
intent.setAction(ACTION_START_NOTIFICATION_SERVICE);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static PendingIntent getDeleteIntent(Context context) {
Intent intent = new Intent(context, NotificationEventReceiver.class);
intent.setAction(ACTION_DELETE_NOTIFICATION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Intent serviceIntent = null;
if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) {
Log.i(getClass().getSimpleName(), "onReceive from alarm, starting notification service");
serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);
} else if (ACTION_DELETE_NOTIFICATION.equals(action)) {
Log.i(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete");
serviceIntent = NotificationIntentService.createIntentDeleteNotification(context);
}
if (serviceIntent != null) {
NotificationIntentService.startService(context);
}
}
}
Методы уведомления IntentService:
private void processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context , CHANNEL_ID);
builder.setContentTitle("Drug Reminder for: " + memName)
.setAutoCancel(true)
.setSmallIcon(R.drawable.drug_icon)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText("Now its time for " + memName +
" to get " + drugName)
.setSmallIcon(R.drawable.logo)
.setChannelId(CHANNEL_ID);
Intent mainIntent = new Intent(this, CourseActivity.class);
mainIntent.putExtra(Intent.EXTRA_TEXT, courseId);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this));
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Create or update. */
NotificationChannel channel = new NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
// mNotificationManager.createNotificationChannel(channel);
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
else {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Drug Reminder for: " + memName)
.setAutoCancel(true)
.setSmallIcon(R.drawable.drug_icon)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText("Now its time for " + memName +
" to get " + drugName)
.setSmallIcon(R.drawable.logo);
Intent mainIntent = new Intent(this, CourseActivity.class);
mainIntent.putExtra(Intent.EXTRA_TEXT, courseId);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this));
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(NOTIFICATION_ID, builder.build());
}
}