【问题标题】:Bad notification for startForeground error when starting a service in android studio在android studio中启动服务时startForeground错误的错误通知
【发布时间】:2023-10-11 15:07:01
【问题描述】:

错误信息是:android.app.RemoteServiceException: Bad notification for startForeground

也不知道是否值得一提,但服务在mac os上正常启动。

我的代码:

public class App extends Application {

    public static final String CHANNEL_ID = "serviceChannel";

    @Override
    public void onCreate() {
        super.onCreate();

        createNotificationChannel();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
public class NotificationActivity extends AppCompatActivity {
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_service);

        editText = findViewById(R.id.editText);
    }
    public void startService(View v) {
        String input = editText.getText().toString();
        Intent serviceIntent = new Intent(this, NotificationService.class);
        serviceIntent.putExtra("inputExtra", input);
        ContextCompat.startForegroundService(this, serviceIntent);
    }
    public void stopService(View v) {
        Intent serviceIntent = new Intent(this, NotificationService.class);
        stopService(serviceIntent);
    }
}

通知服务

public class NotificationService extends Service {


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Reminder")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_android)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

【问题讨论】:

  • 我想你忘了设置NotificationChannel。添加.setChannelId(serviceChannel.getId())
  • 所以我的问题是,我忘记在清单中添加 android 名称: android:name=".notificationService.App" 。我多么愚蠢。谢谢你

标签: android android-service android-notifications foreground-service


【解决方案1】:

所以我的问题是,我忘了在清单中添加 android 名称: android:name=".notificationService.App" 。我多么愚蠢。谢谢你

【讨论】:

    最近更新 更多