Created
October 28, 2021 23:06
-
-
Save navczydev/47e9342169704a50996bdcd91e31069b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ForegroundService : Service() { | |
override fun onCreate() { | |
super.onCreate() | |
} | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
val serviceChannel = NotificationChannel( | |
"myServiceChannel", | |
"My Service Channel", | |
NotificationManager.IMPORTANCE_DEFAULT | |
) | |
val notificationManager = getSystemService(NotificationManager::class.java) | |
notificationManager?.createNotificationChannel(serviceChannel) | |
} | |
val notificationIntent = Intent(this, MainActivity::class.java) | |
val pendingIntent = PendingIntent.getActivity( | |
this, | |
0, | |
notificationIntent, | |
PendingIntent.FLAG_IMMUTABLE | |
) | |
val notification = NotificationCompat.Builder(this, "myServiceChannel") | |
.setContentTitle("foregroundServiceNotificationTitle") | |
.setContentText("input") | |
.setSmallIcon(R.drawable.ic_launcher_nav) | |
.setContentIntent(pendingIntent) | |
// todo don't wait for 10 seconds | |
//.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE) | |
.build() | |
startForeground(1, notification) | |
return START_NOT_STICKY | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
} | |
override fun onBind(intent: Intent?): IBinder? { | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment