前台服务类错误通知

时间:2019-11-10 18:47:59

标签: java android service notifications

我正在尝试在我的应用程序中启动前台服务。我被告知,对于api级别28及更高版本的服务,每当启动服务时都需要通知。下面是我在服务类的onstartcommand中使用的代码

Intent notif=new Intent(this,MyService.class);

PendingIntent  pendingIntent=PendingIntent.getActivity(this,0,notif,0);
  Notification notification=new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher_round)
    .setContentText("w")
    .setContentTitle("jj")
    .setContentIntent(pendingIntent).build(); 
 startForeground(200,notification);

这样做时,我会收到错误消息

  

startForeground的错误通知:java.lang.RuntimeException:服务通知的无效通道:Notification(channel =我的通道pri = 0 contentView = null

我看到了与此相关的帖子,例如 Bad notification for start Foreground, Invalid channel for service notification:,但它们都包含对android 26及更高版本的检查。鉴于我的最小sdk版本是16,最大sdk版本是28,解决此错误的最佳方法是什么。这是我第一次使用服务和通知

这是我第一次使用服务和通知。

0 个答案:

没有答案