I used calling functionality in my application. when the app is in the foreground then the application is working well. but when the app is in the background then how to open incoming call Activity in android. When a push notification is appeared then open incoming call Activity in android. how to perform this task?
CodePudding user response:
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
https://developer.android.com/guide/components/activities/background-starts
CodePudding user response:
Notifications may have a pending intent attached. This pending intent may start an activity. See Notification.Builder.setContentIntent()
CodePudding user response:
You can add a Pending Intent to your notification, so when user clicks on it, your intent will be launched.
To create a Pending Intent you should:
Create a normal Intent to your destination Activity.
val goActivity = Intent(context, Activity::class.java)
Create a Pending Intent passing a context, a request code to identify the Pending Intent, your normal Intent and a flag to configure the way your Intent is launched.
val goActivityPending = PendingIntent.getActivity(context, pendingIntentCode, goActivity, PendingIntent.FLAG_NEW_TASK)
Add your Pending Intent to your notification builder with method
setContentIntent
.val notification = NotificationCompat.Builder(context, channelId) .setContentTitle(context.getString(R.string.notificationTitle)) .setContentText(context.getString(R.string.notificationContent)) .setContentIntent(goActivityPending)
That's it! When you click the notification, your activity will be oppened. You can also use addAction()
to add buttons to your notification with different Pending Intents.
If you want to know more about Intent flags, read this: Pending Intent Flags