Bad notification for startForeground

时间:2025-02-08 13:42:13

处理步骤:

  1. 权限问题 :

      <uses-permission android:name=".FOREGROUND_SERVICE" />
    
  2. Android 8.0以上需要Notification需要设置个Channel

    下面2处都需要注意设置channelId

    2.1: 接收通知

         builder;
                     if (.SDK_INT >= Build.VERSION_CODES.O) {
                         if (!) {
                              = true;
                             NotificationChannel chan = new                   NotificationChannel(UmengMessageHandler.PRIMARY_CHANNEL, (context).getNotificationChannelName(), NotificationManager.IMPORTANCE_DEFAULT);
                             NotificationManager manager = (NotificationManager) (Context.NOTIFICATION_SERVICE);
                             if (manager != null) {
                                 (chan);
                             }
                         }
                         builder = new (context, UmengMessageHandler.PRIMARY_CHANNEL);
                     } else {
                         builder = new (context);
                     }
    

    2.2: 服务 oncreate

         if (.SDK_INT >= Build.VERSION_CODES.O) {
         notification = new (this, UmengMessageHandler.PRIMARY_CHANNEL).getNotification();
          |= Notification.FLAG_NO_CLEAR;
         startForeground(1, notification);
     }
    

服务启动注意:

if (.SDK_INT >= Build.VERSION_CODES.O) {
            (new Intent(context, ));
        } else {
            (new Intent(context, ));
        }