28

In Android 10 there apply new restrictions for apps. We can no longer start an activity from background. While this may be fine for the majority of apps, it's a killing blow for voip-apps that need to show an incoming call after a push notification arrived.

According to this https://developer.android.com/guide/components/activities/background-starts there is a list of conditions that can be met to still allow opening an activity, but tbh I do not understand that fully (non-english-native here).

What I definitely know, is:

  • I do not have any running activity, task, backstack and the like

  • The app is NOT EVEN RUNNING

What I need to achieve:

  • The FCM service of the app receives a push from our server and shall present the incoming call screen (over lock screen and all - just as it did with android 9 and below)

What can I do to open an activity for an incoming voip call in android 10? Over the lockscreen and all, just as a normal user would expect from a PHONE app.

Thanks in advance for any hints.

Grisgram
  • 3,105
  • 3
  • 25
  • 42

4 Answers4

12

To open Activity over lock screen. you can use a high-notification with "full-screen intent" as CommonsWare's answer. But for more detail, you can try my solution as below code:

  1. Create a foreground service then call buildNotification in onStartCommand method, the buildNotification method will return a notification which put into startForeground method parameter.

     public class IncomingCallService extends Service {
         public int onStartCommand(Intent intent, int flags, int startId) {
             Notification notification = buildNotification();
             startForeground(1, notification);
             return START_NOT_STICKY;
         }
     }
    
  2. In buildNotification method, we will create notification with high priority, call category and a full screen intent.

     private Notification buildNotification() {
         Intent fullScreenIntent = new Intent(this, IncomingCallActivity.class);
         PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
         NotificationCompat.Builder notificationBuilder =
             new NotificationCompat.Builder(this)
                     .setSmallIcon(R.drawable.ic_notification_icon)
                     .setContentTitle("Incoming call")
                     .setContentText("(919) 555-1234")
                     .setPriority(NotificationCompat.PRIORITY_HIGH)
                     .setCategory(NotificationCompat.CATEGORY_CALL)
                     // Use a full-screen intent only for the highest-priority alerts where you
                     // have an associated activity that you would like to launch after the user
                     // interacts with the notification. Also, if your app targets Android 10
                     // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
                     // order for the platform to invoke this notification.
                     .setFullScreenIntent(fullScreenPendingIntent, true);
         notificationBuilder.setAutoCancel(true);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             notificationManager.createNotificationChannel(new NotificationChannel("123", "123", NotificationManager.IMPORTANCE_HIGH));
             notificationBuilder.setChannelId("123");
         }
         Notification incomingCallNotification = notificationBuilder.build();
         return incomingCallNotification;
     }
    
  3. In onStartCommand, add a line of code to send ACTION_CLOSE_SYSTEM_DIALOGS broadcast action. This verify IMPORTANT to kick off full screen pending intent.

     public int onStartCommand(Intent intent, int flags, int startId) {
         Notification notification = buildNotification();
         startForeground(1, notification);
         sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
         return START_NOT_STICKY;
     }
    
  4. Create full screen activity which you want to display over lock screen then you need to add setShowWhenLocked and setTurnScreenOn for display over lock screen. If not, your activity will be displayed behind lock screen. Below is my sample.

     public class IncomingCallActivity extends AppCompatActivity {
         protected void onCreate(@Nullable Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_explore);
             setShowWhenLocked(true);
             setTurnScreenOn(true);
             getWindow().addFlags(
             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                     | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                     | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                     | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                     | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
         }
     }
    
  5. Now you must start IncomingCallService when you receive a call from your logic.

     public void startCallService() {
         Intent intent = new Intent(context, IncomingCallService.class);
         startForegroundService(intent);
     }
    
  6. You must declare activity, service and some permission in your manifest as below:

     <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <application
        ...>
         <activity android:name=".IncomingCallActivity" />
         <service
             android:name=".IncomingCallService"
             android:enabled="true"
             android:exported="true" />
     </application>
    

I tested on google, samsung, vsmart phone. It work well. But for xaomi device. you need to enable some permission by flow below steps:

  1. Long click to you app icon
  2. Open app info
  3. Click to "Other permission" item
  4. Allow show on Lock screen

Now your app will work on xaomi device. If you face any problems with my solution, please leave a comment here. I will help you If I could.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Trung Đoan
  • 643
  • 7
  • 18
  • Thanks for taking the time for such a detailled answer - I will look into it – Grisgram Feb 05 '21 at 08:25
  • 2
    My solution was uploaded on github. you can take a look at https://github.com/doanpt/PhoneActivity – Trung Đoan Feb 05 '21 at 10:28
  • for test. after install app. open cmd or terminal then run command: adb shell am start-foreground-service com.ddona.call/.IncomingCallService – Trung Đoan Feb 05 '21 at 10:29
  • Be careful - getWindow().addFlags() should be called before SetContentView. And it's probably not good to set these flags on API >= 27 - but not sure – Michal Dobrodenka Feb 17 '21 at 12:15
  • @MichalDobrodenka, 1) Actually, my example might work without adding flags. But I added it to work on API below 27. 2) For addFlags method, Google said that It should be set before setContentView for some Flags, not all flags then for the best, I agree that we should move the addFlags before call setContentView – Trung Đoan Feb 18 '21 at 09:29
  • Isn't there any way to ask permission for Xiaomi devices programmatically? – Xoltawn Jun 21 '21 at 05:32
  • Intent.ACTION_CLOSE_SYSTEM_DIALOGS is only available for system apps, how to use it? – Tamim Attafi Jan 26 '23 at 12:47
  • You can't start a foreground service from the background according to Android 12 service restrictions, is there any new solution? – Tamim Attafi Jan 31 '23 at 20:45
11

Use a high-priority notification with a "full-screen intent". That will:

  • Invoke your "full-screen intent" if the device is locked
  • Otherwise, display a "heads-up" notification
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for this. It pointed me in the right direction. I think this will work, together with an expanded notification with a custom layout in case, the app is currently in foreground (using a foreground service). – Grisgram Nov 13 '19 at 18:33
  • 1
    A quite good tutorial can be found here: https://developer.android.com/training/notify-user/time-sensitive for the records. Contains everything needed to solve this. – Grisgram Jan 22 '20 at 17:51
  • With this approach is that possible to show only the full screen intent but not the notification? I implemented this solution but it makes the notification sound when my full screen intent shows up. Description of setFullScreenIntent says: "An intent to launch instead of posting the notification to the status bar.". I don't think it's true. It stars the full screen intent and also creates the notification. – Mitulát báti Apr 06 '20 at 00:14
  • @Mitulátbáti: "is that possible to show only the full screen intent but not the notification?" -- no. "It stars the full screen intent and also creates the notification" -- that is now what the quote says. The quote says that it is used "instead of **posting the notification to the status bar**" (emphasis added). Presumably, other effects, such as ringtone and vibration, are still eligible. If users do not want those effects, users should be able to disable them for your channel in the Settings app. – CommonsWare Apr 06 '20 at 11:41
  • @CommonsWare can you also mention how can we set different colors to notification actions (like red for decline and green for answer) – Liya Dec 09 '21 at 11:07
  • @Liya: Off the top of my head, I do not know of a way to do that -- sorry! – CommonsWare Dec 09 '21 at 12:00
3

Please go through my blog on how to open activity for OS 10 and also how to display heads up notification and handle clicks on the action buttons.

https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4

Lloyd Dcosta
  • 289
  • 2
  • 11
  • I followed your example to the letter, but it didn't work as you showed. Notification remained a normal notification - there was no big pop-up, and there was no full screen intent. – Yazid Mar 31 '20 at 15:20
  • 1
    @YazidEF While creating the notification channel set the priority to high. – Lloyd Dcosta May 28 '20 at 14:24
  • @LloydDcosta i went over your example as well but after i click on the button, HeadsUpNotificationActionReceiver is not called. Any idea why? – alext Nov 19 '20 at 14:04
  • 1
    Your example is great and helpfull, but unfortunately these days when targeting sdk 31 (must) - Intent.ACTION_CLOSE_SYSTEM_DIALOGS is obsolete and app crashes if used. – Michal Dobrodenka Dec 15 '22 at 12:31
1

Check this link this will help you here

or

You need to ask for a permission "draw over other app" then you can make this as previous versions

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (!Settings.canDrawOverlays(this)) {
                RequestPermission();

        }
        }

    private void RequestPermission() {
        // Check if Android P or higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Show alert dialog to the user saying a separate permission is needed
            // Launch the settings activity if the user prefers
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + BuildConfig.APPLICATION_ID));
            startActivityForResult(intent, 
            ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
        }
     }

or You can use my this answer

https://stackoverflow.com/a/63699960/7108113

Dinil ps
  • 321
  • 3
  • 10