Created
October 3, 2019 06:07
-
-
Save alishir/a59bbc552a16711c34ff7d98dc9558f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MyService : Service() { | |
| companion object { | |
| const val TAG = "MyService" | |
| const val CHNNEL_ID = "my-service" | |
| } | |
| override fun onCreate() { | |
| super.onCreate() | |
| Log.i(TAG, "onCreate") | |
| // create notification channel for API 26+ | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| val channel = NotificationChannel(CHNNEL_ID, CHNNEL_ID, NotificationManager.IMPORTANCE_DEFAULT).apply { | |
| description = "port sip service" | |
| } | |
| val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
| notificationManager.createNotificationChannel(channel) | |
| } | |
| showServiceNotification() | |
| } | |
| private fun showServiceNotification() { | |
| Log.i(TAG, "showServiceNotification") | |
| val pendingIntent = Intent(this, MainActivity::class.java).let { | |
| PendingIntent.getActivity(this, 0, it, 0) | |
| } | |
| var builder = NotificationCompat.Builder(this, CHNNEL_ID) | |
| .setSmallIcon(R.drawable.ic_notification) | |
| .setContentTitle("MY Service") | |
| .setContentText("Service is running.") | |
| .setPriority(NotificationCompat.PRIORITY_DEFAULT) | |
| .setContentIntent(pendingIntent) | |
| startForeground(123, builder.build()) | |
| } | |
| override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
| Log.i(TAG, "onStartCommand") | |
| return super.onStartCommand(intent, flags, startId) | |
| } | |
| override fun onBind(p0: Intent?): IBinder? { | |
| return null | |
| } | |
| override fun onDestroy() { | |
| super.onDestroy() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment