31

Notification trampoline restrictions-Android12

 2 years ago
source link: https://proandroiddev.com/notification-trampoline-restrictions-android12-7d2a8b15bbe2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Notification trampoline restrictions-Android12

Did you ever notice?

  • Sometimes tapping a notification 🔔 and nothing happens for a couple of minutes, then it suddenly pops up? 😬

Starting with Android 12 notifications will not work if they do not start activities directly.

What is a Notification trampoline?

Some apps respond to notification taps by launching an app component (Service, BroadcastReceiver) that starts the activity that the user finally sees and interacts with. This app component is known as a notification trampoline.

Android12 restrictions

  • Applications that target Android 12 or higher can’t start activities from services or broadcast receivers that are used as notification trampolines.

Or

  • In simple words after the user taps on a notification or an action button within the notification, the app cannot call startActivity() inside of a service or broadcast receiver which helps to improve the app performance and user experience.

Error message in logcat

The system prevents the activity from starting when an application tries to start an activity from a service or broadcast receiver that acts as a notification trampoline, and the following message appears in Logcat:

NotificationTrampolineErrorMesage.kt

Sample code that will create this issue

  • Trampoline component: Here we have a BroadcastReceiver that will start an activitywhen the user taps on the notification action.
NotificationReceiver.kt
  • Notification code:
NotificationWithReceiverIntent.kt

Identify which app components act as notification trampolines

  • During the testing of the application, after you tap on a notification, you can identify which service or broadcast receiver acted as the notification trampoline in your app.
  • To do so, look at the output of the following terminal command:
CommandToFindNotificationTrampolines.kt

Toggle the behavior

During the testing of a debuggable version of the application, we can enable and disable this restriction using the NOTIFICATION_TRAMPOLINE_BLOCK app compatibility flag.

You can find it under the developer options

1*18wGPdXZ2JmEnGG1xEZRSQ.png?q=20
notification-trampoline-restrictions-android12-7d2a8b15bbe2
App compatibility flag

Update your app

  • If you find that your application starts an activity from a service or broadcast receiver that acts as a notification trampoline, complete the following migration steps :
  1. Create a PendingIntent object that is associated with the activity that users see after they tap on the notification.
  2. Use the PendingIntent object which is created in the previous step as part of building your notification.
NotificationBuilderSample.kt

😊😊 👏👏👏👏 HAPPY CODING 👏👏👏👏 😊😊


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK