简体   繁体   中英

Unable to start a foreground service using kotlin multiplatform

So i am trying to start a simple foreground service, but seems like i am doing something wrong, or maybe there is a different way to do it using fragment.

So this is my service:

class LocationForegroundService : Service() {

    companion object {
        private const val TRACKING_CHANNEL = "tracking_channel"
        private const val TRACKING_NOTIFICATION_ID = 1

        fun startService(context: Context, message: String) {
            val startIntent = Intent(context, LocationForegroundService::class.java)
            startIntent.putExtra("inputExtra", message)
            ContextCompat.startForegroundService(context, startIntent)
        }

        fun stopService(context: Context) {
            val stopIntent = Intent(context, LocationForegroundService::class.java)
            context.stopService(stopIntent)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        val input = intent?.getStringExtra("inputExtra")
        createNotificationChannel()

        val notificationIntent = Intent(this, RiderManagementActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            notificationIntent,
            0
        )

        val notification = NotificationCompat.Builder(this, TRACKING_CHANNEL)
            .setContentTitle("Test")
            .setContentText(input)
            .setContentIntent(pendingIntent)
            .build()

        startForeground(TRACKING_NOTIFICATION_ID, notification)

        return START_STICKY
    }

    private fun createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(
                TRACKING_CHANNEL,
                "Foreground Location Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val manager = getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(notificationChannel)
        }
    }

    override fun onBind(intent: Intent?): IBinder? = null
}

and this is how a call the service in the fragment:

binding.btnPlayPause.setOnClickListener {
            val intent = Intent(requireContext(), LocationForegroundService::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                requireContext().showShortToast("track")
                LocationForegroundService.startService(requireContext(), "Testing")
            } else {
                requireActivity().startService(intent)
            }
        }

and i had declared on manifest file:

...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
...
<application
       .../>

       <service
              android:name=".util.LocationForegroundService"
              />

So i must be missing something because i am getting this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.quicktendr.mgmt.androidApp, PID: 12693
    android.app.RemoteServiceException: Bad notification for startForeground
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1872)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

Updating: I created 2 others projects and tested the same service. 1 android native and the other in kotlin multiplataform like the original, and on the android one worked fine, but the kotlin multiplataform i get the same error.

As far as I am aware, you need to create your notification channels before you attempt to start a foreground service (>= API 27).

So trying to create your channels within your service won't work.

I solved this error adding an icon to the notification, but it is weird just crash on a kotlin multiplatform project but no on android project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM