0

Problem - So here is the thing, Trying to get notification appear from the bottom side of the app while the app is in killed state or it can be anything that can act like a notification that has very high priority but should appear from the bottom side, and it should be done with WorkManager, that makes it appear every day at specified time

Did a lot research and as you can see could not find any relevant information that solves my little problem.

I have tried - To show a snackBar in doWork method of the Worker Class

class NotifyWork(context: Context, params: WorkerParameters) : Worker(context, params) {
    
    override fun doWork(): Result {
        
        // val mySnackbar = Snackbar()
        
        val id = inputData.getLong(NOTIFICATION_ID, 0).toInt()
        sendNotification(id)
        return success()
    }

    private fun sendNotification(id: Int) {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
        intent.putExtra(NOTIFICATION_ID, id)

        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val notificationLayoutExpanded =
            RemoteViews(applicationContext.packageName, R.layout.layout_notification)
        val bitmap = applicationContext.vectorToBitmap(R.mipmap.ic_launcher)
        val pendingIntent = getActivity(applicationContext, 0, intent, 0)
        
        val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
            .setLargeIcon(bitmap)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setAutoCancel(true)
            .setPriority(PRIORITY_MAX)
            .setCustomContentView(notificationLayoutExpanded)
            .setOngoing(true)
            .setFullScreenIntent(pendingIntent, true)
            .setContentIntent(pendingIntent)
        notification.priority = PRIORITY_MAX

        if (SDK_INT >= O) {
            notification.setChannelId(NOTIFICATION_CHANNEL)

            val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
            val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
                .setContentType(CONTENT_TYPE_SONIFICATION).build()

            val channel =
                NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)

            channel.enableLights(true)
            channel.lightColor = RED
            channel.enableVibration(true)
            channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            channel.setSound(ringtoneManager, audioAttributes)
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(id, notification.build())
    }

    companion object {
        const val NOTIFICATION_ID = "appName_notification_id"
        const val NOTIFICATION_NAME = "appName"
        const val NOTIFICATION_CHANNEL = "appName_channel_01"
        const val NOTIFICATION_WORK = "appName_notification_work"
    }
}

This current code shows notification with high priority on top of the screen

So the question is - IS there any way to show anything that acts like a notification with high priority and appears from bottom when app's state is killed ?

it should be similar to this picture

This is the sample picture of how it should look

  • Have you tried using a bottom sheet dialog fragment? – Astrit Veliu Aug 25 '21 at 12:28
  • No, but I think when app is in killed state it is not possible to use bottom sheet dialog fragment, if it is possible let me know, I need to use WorkManager to trigger this action when app is in killed state – Bakai Ismailov Aug 26 '21 at 08:47
  • Can you describe a little more in details your use case, when you trigger this action? I had something similar when device restarts to show a notification, showing the bottom sheet dialog fragment can be similar. – Astrit Veliu Aug 26 '21 at 14:06
  • So my app should show notification every day at 20 : 00, if app is actively running or app is in killed state. I have managed to do it with WorkManager that triggers doWork method of Work class, every day at 20 : 00, no matter the app is in killed state or actively running, it will show notification from the top side of the phone screen (high priority notification)

So the task is to show the same thing from the bottom every day at 20 : 00 I have link to a picture how it should be displayed plz check the link “This is the sample picture of how it should look” – Bakai Ismailov Aug 26 '21 at 18:05

1 Answers1

0

I think what you need is a bottomSheet in android it appears from the bottom. When the app is killed the onDestroy(){check comments} is called, so in there you can call the bottomsheet. And to make sure that it does not get killed with the app: https://stackoverflow.com/a/52258125/15552614 Look at this one.

Mihir Shah
  • 51
  • 3
  • 1
    From the [Android Developers](https://developer.android.com/reference/android/app/Activity.html#onDestroy()) website: *Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.*. So `onDestroy()` is not guaranteed to be called, thus is not a reliable place to put such code. Using a service would be more appropriate, since killing the application would also stop the service and this event can be catched. – Armaggheddon Aug 25 '21 at 11:02
  • Thank you for your answer! I Described it a little mistakenly, so what I meant to describe is I wanted to make appear notification like thingy, WHILE THE APP IS IN KILLED STATE ALREADY As you know notification with high priority appears from top even the app is in killed state by schedule, if you have any idea plz let me know – Bakai Ismailov Aug 26 '21 at 05:27