可能的其他原因是什么
Fatal Exception: android.app.RemoteServiceException
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification
是否没有设置频道?它似乎仅在Android 8和9上发生
我的堆栈跟踪显示该通道具有一个值:
invalid channel for service notification: Notification(channel=com.myapp.notifications pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x52 color=0x00000000 category=service number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
因此似乎已正确创建了频道。
我的后台服务设置为常规
public static final String NOTIFICATION_CHANNEL_ID = "com.myapp.notifications";
public static final String SERVICE_CHANNEL_ID = "com.myapp.services";
public static final int NOTIFICATION_ID = 100;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(2, buildNotification(getApplicationContext()));
}
}
@RequiresApi(Build.VERSION_CODES.O)
Notification buildNotification(Context context) {
String channelId = SERVICE_CHANNEL_ID;
setupNotificationChannel(context, channelId);
return NotificationCompat.Builder(context, channelId)
.setOngoing(true)
.setSmallIcon(R.drawable.app_icon)
.setCategory(Notification.CATEGORY_SERVICE)
.setAutoCancel(true)
.setChannelId(channelId)
.build();
}
@RequiresApi(Build.VERSION_CODES.O)
void setupNotificationChannel(Context context, String channelId) {
NotificationManager notificationManager = getNotificationManager(context);
if (notificationManager.getNotificationChannel(channelId) == null) {
NotificationChannel channel = NotificationChannel(channelId, getChannelName(channelId), getChannelImportance())
channel.setDescription(getChannelDescription(channelId))
notificationManager.createNotificationChannel(channel)
}
}
我还以类似的方式显示一些推送通知:
public void showNotification(Context context) {
NotificationManager notificationManager = getNotificationManager(context);
String channelId = SHRNotificationService.NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannel(context, channelId);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle(getNotificationTitle())
.setColor(ContextCompat.getColor(context, R.color.BLUE))
.setChannelId(channelId)
.setPriority(getNotificationPriority(channelId))
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
我有
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
在清单中。
还不清楚的是,堆栈跟踪是指来自 service 类别的通知,其中 com.myapp.notifications 作为通道ID,但没有背景服务或通知均符合这两个条件。
答案 0 :(得分:0)
已经有一段时间了,但是我认为这是您想要使它看起来像的样子:
fun sendToForegroundWithNotification() {
val CHANNEL_ID = "YourChannelId"
@Suppress("DEPRECATION") //for the NotificationBuilder < API26 ctor
val notificationBuilder: Notification.Builder
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// O > need a channel, create one
notificationManager.createNotificationChannel(
NotificationChannel(
CHANNEL_ID,
"Title",
NotificationManager.IMPORTANCE_DEFAULT
)
)
notificationBuilder = Notification.Builder(this, CHANNEL_ID)
} else notificationBuilder = Notification.Builder(this)
val notification = notificationBuilder
.setContentTitle(getText(R.string.app_name))
.setContentText(getText(R.string.your_content))
.setSmallIcon(R.drawable.some_icon)
.etc(whatever you need)
.build()
// since you're in your Service, you can call startFg directly:
startForeground(666, notification) // ;-)
}