我从线程创建通知:
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel chan = new NotificationChannel("MyNotificationChannel","Notification Title", NotificationManager.IMPORTANCE_HIGH);
chan.setSound(null, null);
manager.createNotificationChannel(chan);
final Notification notification =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? new Notification.Builder(context, "MyNotificationChannel") : new Notification.Builder(context))
.setContentTitle(context.getString(R.string.app_name))
.setContentText("Text")
.setSmallIcon(R.drawable.logo)
.setFullScreenIntent(PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT), true)
.setCategory(Notification.CATEGORY_ALARM)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.build();
((Service) context).startForeground(12345678, notification);
当我尝试在活动销毁时删除该通知时,它适用于大多数设备,但在某些摩托罗拉设备或搭载 Android 10 的小米设备上:
protected void onDestroy() {
try {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(12345678);
mNotificationManager.deleteNotificationChannel("MyNotificationChannel");
}catch(Exception e){
e.printStackTrace();
}
}
此异常已通过,并且通知未删除,我尝试在线程中删除并进入另一个活动:
java.lang.SecurityException: Not allowed to delete channel "MyNotificationChannel" with a foreground service
其他地方也有类似的帖子。对我有用的是,我使用 try catch 环绕块巧妙地捕获了异常并继续前进。最佳做法是在 FG 服务正在使用该频道时不要删除该频道,或者在继续删除该频道之前停止该服务
经过多次测试,我找到了解决方案。 我将其添加到 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.REORDER_TASKS" />
服务中:
boolean locked = false;
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
locked = true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && locked) {
final NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
try {
manager.cancel(12345678);
}catch(Exception e){
e.printStackTrace();
}
NotificationChannel chan;
final NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channel = "MyNotificationChannel";
chan = manager.getNotificationChannel(channel);
if (chan == null){
chan = new NotificationChannel(
channel,
"Notification Title",
NotificationManager.IMPORTANCE_HIGH);
chan.setSound(null, null); // Service manages its own sound.
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager.createNotificationChannel(chan);
}
PendingIntent pending = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
final Notification notification =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
new Notification.Builder(context, channel) :
new Notification.Builder(context))
.setContentTitle(context.getString(R.string.app_name))
.setContentText("Notification text")
.setSmallIcon(R.drawable.log_trans_mini)
.setFullScreenIntent(pending, true)
.setContentIntent(pending)
.setCategory(Notification.CATEGORY_ALARM)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setLights(Color.WHITE, 1000, 1000)
.build();
((Service) context).startForeground(12345678, notification);
} else {
context.startActivity(myIntent);
}
接下来需要做的是尽可能删除设备中的频道,因为现在在您收到通知后该频道始终存在。在 moto g6 中我仍然无法删除频道,但会显示通知。
允许删除 mp3 果汁下载上的频道