我正在使用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版本...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
我还设置了通知频道:
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
但是一切都不会改变任何事情...
我不知道该怎么办。 请提供任何帮助,谢谢。
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);
}
}
}
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());
}
}