Android S Notification on click intent not working - android

i am building a flutter plugin in java that creates a media notification, and i am having a problem getting the notification click to bring the app to foreground, it used to work on previous android versions but not when testing it on S (31) as a target.
if found this section in the docs :
As of Android Build.VERSION_CODES.S, apps targeting API level Build.VERSION_CODES.S or higher won't be able to start activities while processing broadcast receivers or services in response to notification clicks. To launch an activity in those cases, provide a PendingIntent for the activity itself.
I would like to know if thi is the case or not, or if i need to check something else, here is the code i am using to create the intent i set in the notification builder :
private PendingIntent createContentIntent() {
Intent openUI = new Intent(mContext, AudioplayerPlugin.class);
openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(mContext, REQUEST_CODE, openUI,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
}
AudioplayerPlugin is the plugins own name, the app itself is another activity.

Related

PendingIntent send() not working on Android 12 when application is closed

I'm trying to show an Activity in onMessageReceived of my FirebaseMessagingService. I've already asked user for permission and checked that it granted.
For Android <=11 simple start activity method worked correctly. Also, it works correctly for Android 12 for emulator.
context.startActivity(Intent(context, MyActivity::class.java).apply {
// add extra
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
})
But for real devices (Samsung and Xiaomi) with Android 12 such approach is not working.
Also, I've tried PendingIntent without result:
val pendingIntent = PendingIntent.getActivity(
context,
0,
Intent(context, MyActivity::class.java)
.apply { // add extra },
PendingIntent.FLAG_IMMUTABLE
)
pendingIntent.send()
Both approaches works, if the application is running. Any suggestions?
If your application is in the background the notification is delivered to the device's system tray and is not handled by onMessageReceived.
Notifications handled by the system tray should just open your app.
More information can be found here

Android 12 - About Correspondence to notification trampoline

We are working on notification trampolines on Android 12.
Originally our app launches an activity by a broadcast receiver.
I found out that using PendingIntent.getActivity instead of PendingIntent.getBroadcast would solve the problem.
Regarding this, I have a following concern.
When the broadcast receiver is used, i.e. when PendingIntent.getBroadcast is used, I programmed so that the broadcast receiver determines whether to launch the app.
However, I no longer use the broadcast receiver due to notification trampolines. Therefore, PendingIntent.getActivity launches the app without choice.
I would like to know if there is any way to determine whether to launch the app depending of the state of app without using the broadcast receiver.
For example;
when App is in state A:Launch the app with a push notification tap
when App is in state B:NOT launch the app with a push notification tap
sort of workaround would be to launch some dedicated Activity, which may be set as fully transparent without any enter/exit animation, noHistory flag etc. and in there you may run your checking logic - starting "real" Activity or just finish() if there is no need
I'm using a transparent activity to handle this issue. all the notification related works are handled in the transparent activity.
Intent intent = new Intent(mContext, NotificationActivity.class);
intent.putExtra("notification", parseInt(this.mActionDetail.getNotifyId()));
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
notificationManager.notify(parseInt(this.mActionDetail.getNotifyId()), builder.build());
create a transparent activity NotificationActivity.class then you can identify the application state then you can decide the action

Notification with button that upon click take an action without opening activity

I am trying to create a notification where I add button to it that would basically do some action. I know I can do the following
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(con)
.setSmallIcon(image)
.setContentTitle("title")
.addAction(icon, title, intent)
My questions are:
1) Is adding button supported in API 5.0+ ONLY or also in 4.x? I read different answers about it
2) The action seems to be associated with opening an activity. Is there away I can have it so when you click on a button it takes an action without having to open the activity (Either through broadcast receiver or some other way)? As far as I know Intent opens activities.
Thank you so much
It will work in Android 4.1 and later. See official doc.
If you want to do action with out any UI update(i.e., showing any activity), I suggest send a pending intent(which will trigger a broadcast receiver) as a parameter for notification action
i) Create a BroadcastReceiver named MyBroadcastReceiver
ii) Add your action in BroadcastReceiver's onReceive method
iii) Create a PendingIntent
Intent mIntent = new Intent(this,MyBroadcastReceiver.class);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent , 0);
iv) Add it to Notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(con)
.setSmallIcon(image)
.setContentTitle("title")
.addAction(icon, title, mPendingIntent)
1) Is adding button supported in API 5.0+ ONLY or also in 4.x?
It will work in Android 4.1 and later. See official doc.
2)...As far as I know Intent opens activities.
Yes, you can use BroadcastReceiver or Service for executing logic that doesn't involve UI. First of all, you can build intent to launch activity, broadcast receiver, or service. Secondly, the third argument of NoticiationCompat.Builder#addAction is PendingIntent, not an Intent. You can use PendingIntent.getService to create an PendingIntent for service, for instance.
http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context, int, android.content.Intent, int)

how to call activity from service?

I have created two projects. One project will act as a library and another as a main project. I will call the main project to library project, then the library project will send a notification. It's working fine but my question is if i click the notification it will go to main project activity. What can i do? Is it possible in android?
In library project :
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent("com.sample.myapp.Monitor"), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Start Scheduler")
.setContentText("Please start your activity!")
.setContentIntent(contentIntent).setAutoCancel(true)
.setLights(Color.RED, 1, 1);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
here the com.sample.myapp.Monitor is main project activity.If i click the notification it does not call main project activity.
Finally i got the solution to connect my library project to main project activity.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
I'm not sure if I understood you correctly, so you want to create a notification and when user clicks it you want to show him your Activity, right? In this case you can read Google documentation about that
EDIT: Here you can look a good example of creating a Service with BroadcastReceiver. I assume that your library does some background processing (perhaps in Service as well). So I would suggest the following scenario:
Your application starts a service with registered BroadcastReceiver
Your library project does some background processing and after that sends a broadcast as described in the example provided.
Previosely registered BroadcastReceiver receives a broadcast and sends a notification with action pointing to your Activity (as described in the developer page from my original answer)
User click notification and Activity opens.

Change notification intent in Android

I have a service that shows a notification that I wish that will be able to go to a specific activity of my app each time the user presses on it. Usually it would be the last one that the user has shown, but not always.
If the activity was started before, it should return to it, and if not, it should open it inside of the app's task, adding it to the activities tasks.
In addition, on some cases according to the service's logic, I wish to change the notification's intent so that it will target a different activity.
How do i do that? Is it possible without creating a new notification and dismissing the previous one? Is it also possible without creating a new task or an instance of an activity?
No it wouldn't be possible to change the Activity once you have sent the notification.
You can start an Activity on your task stack that is not a problem, check out the notification service in the tutorial here:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
You have to set a pending intent on the notification:
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
// Set the info for the view that shows in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
You can see the pending intent takes a normal intent "new Intent(this, SecondActivity.class" so if you want specific behaviour (like bringing to the top instead of starting a new activity. Add the flags like you would normally to this intent. i.e. FLAG_ACTIVITY_REORDER_TO_FRONT (something like that)
Since platform version 11, you can build a notification using Notification.Builder. The v4 support library has an equivalent class NotificationCompat.Builder.
You can't change the Activity once you've sent the notification, but you can update the notification with a new Intent. When you create the PendingIntent, use the flag FLAG_CANCEL_CURRENT. When you send the new notification, use the ID of the existing notification when you call NotificationManager.notify().
Also, you should be careful how you start your app. The Status Bar Notifications guide tells you how to set up the back stack.

Categories

Resources