How to choice activity to open when notification clicked - android

Is there way to choice the activity to start just in moment of clicking notification?
I can use setContentIntent when I build notification. But being once shown it seems not possible to set or change content intent in moment of click...

The work-around I would suggest is to direct the setContentIntent to a simple activity , which handles you specific cases in the onCreate() with the needed intent of your choice.

try this
Intent intent = new Intent(this, YOURACTIVITY.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent);

Related

Pending Intent for more than one notification

I have app which sends notifications, I used this code for pending intent
Intent myIntent = new Intent(getApplicationContext(),MainActivity.class);
myIntent.putExtra("link",Link);
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),1,myIntent,
PendingIntent.FLAG_ONE_SHOT);
and it worked nice for the first time, but I use this piece of code inside a method that invokes every 1 minute.
The problem is that the link variable changes from one to another.
and when I get the data in the MainActivity I found the last link only, all the notifications created has the last link.
and I don't know how to avoid this.
You are sending the same request code for your pending intents. These codes are defined as the 2nd parameter of your Pending intent declaration
Change
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),1,myIntent,
PendingIntent.FLAG_ONE_SHOT);
To:
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_VALUE_FOR_EVERY_CALL,myIntent,
PendingIntent.FLAG_ONE_SHOT);
If you use the same id, the intent will be reused and you will only get the last data rather than getting new data for every call.
Try This,
PendingIntent contentIntent = PendingIntent.getActivity(GCMNotificationIntentService.this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
In Kotlin
Make a notification
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
For one and more just change the request Code number:
val pendingIntent = PendingIntent.getActivity(context,
System.currentTimeMillis().toInt(),intent, 0)
then
builder.setContentIntent(pendingIntent)

How to open non-launcher activity on notification group click

Currently I have this:
Intent resultIntent = new Intent(context, DigestPager.class);
and this is opening DigestPager activity (which is not launcher activity) when clicking on a single notification.
But when there are multiple notifications and they are grouped into one and collapsed, clicking on it is opening launcher activity.
Is there some simple way to open a different activity?
Intent resultIntent = new Intent(context, DigestPager.class);
resultIntent.putExtra("digestId", digest.getDigestId());
PendingIntent pendingIntent = PendingIntent.getActivity(this, digest.getDigestId(), resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(digest.getDigestId(), mBuilder.build());
Create a handler activity that opens everytime you click on a notification and based on the flags set to the notification, you can navigate to the required activity/fragment.
Hope this helps.

Only one Notifcation is clicable from a few

The problem is: in my code im sending 4 notification. But only one top notifacation opens my Activity another 3 do nothing on click.
Here is my code to send Notificaiton. I do it 4 times in my program.
The Notification ID is always diferrent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.element)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messages.get(messageCount).toString());
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(messageCount, notification);
Please Help . Thanks!
The problem is that android "eat" the same intents from pending intent.
You need to make them different using different extra and/or request codes

how to start aplication or bring to front when user clicks on a notification click on android

I'm working whit push notifications.
When a user clicks on a notification I want to start the application in case it is not started, or bring it to front in case it is started.
Thanks
this is the complite code I found the answer here Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
The important things are the flags on the Intent, this will open the app or bring it to front if is opened, or do nothing if you click on the notification while you are browsing the app
implement pending intent.
Code
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
manifest
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
Just set the intent for the notification, this is covered in details in the official API guide.
You do that by creating a PendingIntent.
For example, this will launch a MainActivity when notification is clicked.
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
I don't know whether you're talking about Google Cloud Messaging or not. But if it is a normal Notification then while creating your notification you've to provide Pending Intent. Just put your desired class in Pending intent so that when user clicks on that notification you'll be driven to your so called Application. A snippet :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
After this use this intent while creating notification. It's method is setContentIntent("above intent") and fire your notification with NotificationManager!

Editing notification object?

I have created the notification object using NotificationCompat.Builder and then elsewhere in my service I'd like to update value. What is the right approach to do this? Should I cancel this object and bulid another one?
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText("estimated value:" + String.valueOf(value)) // <---- wanna update this
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.build();
startForeground(1235, noti);
You do not need to cancel your previous notification. Instead, you can simply run the identical code with different content text (in this case, changing value).
For more info, see the Updating Notifications section of the Android docs.

Categories

Resources