DEV Community

Mahmoud Mabrok Fouad
Mahmoud Mabrok Fouad

Posted on

4 2

TIL 1: PendingIntent & WorkManager

Today I learned a.k.a TIL a series of posts where I share staff i learned daily.

PendingIntent

I was developing a Notification-App and used PendingIntent with Notification and faced a case that seems strange.

The case is that PendingIntent open Activity with with extra string but when two Notifications comes with two different strings when clicked on both i got first string of them in two cases while i expect getting two different strings.

        // Create an explicit intent for an Activity in your app
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            putExtra(AppConstants.NOTIFICATION_EXTRA_TITLE, title)
            putExtra(AppConstants.NOTIFICATION_EXTRA_MESSAGE, message)
        }


        val pendingIntent: PendingIntent =
            PendingIntent.getActivity(context,0, intent, 0)

Enter fullscreen mode Exit fullscreen mode

The issue

was that two PendingINtent have same requestCode (second params of getActivity())

The solution

is two have unique/disticnt request code

  val pendingIntent: PendingIntent =
            PendingIntent.getActivity(context, Random.nextInt(), intent, 0)
Enter fullscreen mode Exit fullscreen mode

Random.nextInt() made the trick for me.


WorkManager

To schedule the notification I have used WorkManager but want to make the request be unique so as not to be scheduled each time user open the app.

What I used:

  workManager.enqueueUniquePeriodicWork(
            AppConstants.QUEOTES_NOTIFICATION_WORKER_ID,
            ExistingPeriodicWorkPolicy.KEEP,
            request
        )

Enter fullscreen mode Exit fullscreen mode
  • AppConstants.QUEOTES_NOTIFICATION_WORKER_ID is uniqueWorkName
  • ExistingPeriodicWorkPolicy.KEEP If there is existing pending (uncompleted) work with the same unique name, do nothing. Otherwise, insert the newly-specified work.

Fire Worker Immediately:

I want to make my periodic worker start immediately i.e run for the first time without waiting for the first iteration of the scheduling

Solution

add next to worker request .setInitialDelay(-1, TimeUnit.MINUTES)

make Periodic tasks start after some time:

We may need our periodic worker to start from future time instead of now, with latest WorkManager this is allowed for both OneTimeWorkRequest and PeriodicWorkRequest with help of .setInitialDelay(10, TimeUnit.MINUTES)

Hope this adds new knowledge for you, meet next time.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay