package com.example.doctor.Worker

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.example.doctor.MainActivity
import com.example.doctor.R
import java.nio.file.attribute.AclEntry.Builder

class MyWorker(context: Context, workerParameters: WorkerParameters): Worker(context,workerParameters) {
    companion object{
        const val CHANNEL_ID="channel_id"
        const val NOTIFICATION=1
    }
    override fun doWork(): Result {
        Log.d("doWork","doWork - error")
        showNotification()
        return Result.success()//Возврощаем результат
    }

    @SuppressLint("UnspecifiedImmutableFlag", "MissingPermission", "ObsoleteSdkInt")
    private fun showNotification(){
        val intent = Intent(applicationContext,MainActivity::class.java).apply{
            flags=Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntetn= PendingIntent.getActivity(applicationContext,0,intent,0)

        val notification = Notification.Builder(applicationContext, CHANNEL_ID)
            .setSmallIcon(R.drawable.door)
            .setContentTitle("new task")
            .setContentText("Сообщение 1")//Сообщение
            .setPriority(Notification.PRIORITY_MAX)//Приоритет
            .setAutoCancel(true)//Время отмены
            .setContentIntent(pendingIntetn)//Ожидание завершения


        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            val channelName = "channel name"
            val channelDescription = "channel Description"
            val channelImportance=NotificationManager.IMPORTANCE_HIGH

            //Канал
            val channel = NotificationChannel(CHANNEL_ID,channelName,channelImportance).apply {
                description = channelDescription
            }

            //Менеджер уведомлений
            val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)//Создаем канал с помошью менеджера уведомлений

            with(NotificationManagerCompat.from(applicationContext)){
                notify(NOTIFICATION,notification.build())
            }
        }
    }
}


































