How to open non-launcher activity on notification group click - android

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.

Related

Open Activity on Notification Click - Intent flag issue

I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:
App is not running > Notification comes in > Click on the notification > Activity_T opens.
BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.
The flags I use are the follow
Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);
I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.
I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, "")
.setSmallIcon(R.drawable.my_ic)
.setContentTitle(title)
.setContentText(msg)
.setContentIntent(contentIntent);

Tap on Notification from locked screen opens Wrong Activity first

Below function displays a notification which pending intents. When the screen is unlock and receive notification, below method will create a notification.
When user tap on notification, it opens first ChildActivity, On backpress, it closes child activity and opens MainActivity->subActivity. on backpress closed subActivity and shows MainActivity.
Subactivity will be started based on the Intent action of MainActivity
In the unlock screen, below method gives above result and its perfect as per my requirement.
public void showNotification(Context context, String extraUri){
final Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra(MainActivity.INTENT_EXTRA_ACTIVE, 1232123);
// this will help determine to start subactivity from mainActivity
mainActivityIntent.setAction(MainActivity.MAIN_ACTIVITY_CHANGED);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntent(mainActivityIntent);
final Intent intent = new Intent(context, ChildActivity.class);
intent.putExtra(ChildActivity.EXTRA_URI, extraUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
taskStackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message Body")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Problem
while screen is locked and tap on the notification, it ask to unlock the screen, on unlock, it starts the app. but
very first it shows SubActivity, than on back press it shows ChildActivity and on backpress of ChildActivity it shows MainActivity.
So the order of the Activities are changed.
the correct order is
MainActivity->SubActivity->ChildActivity (TopMost) (happens in unlocked screen)
Thanks in Advance.

Catch two notifications in android

I am creating notifications with following code:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(messageBody)
.setAutoCancel(true)
.setSmallIcon(R.drawable.top_icon)
.setContentText(senderName)
.setTicker(senderName+" ")
.setSound(soundUri);
Intent resultIntent = new Intent(this, LoginActivity.class);
resultIntent.putExtra("gcm_username",senderName);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationID.getID(senderName), mBuilder.build());
When user clicks the notification I am catching it with following code in loginActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gcmUserId= getIntent().getStringExtra("gcm_username");
startAction(gcmUserId);
....
My problem starts here.
Scenario:
1)When app is closed user receives notifications from 2 different users
2)User clicks first notification and app starts then startAction method calls.
3)Then user clicks the second notification
But when user clicks the second notification app has already started so startAction won't be able to call again because it is in the onCreate method.How can catch second notification ?
You can handle it inside onNewIntent(). You would need to override that method inside your activity. The docs say:
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

Replace current top activity from intent

In my Android app I send Notification like that:
Intent intent = new Intent(ctx, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, myActivityId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int icon = R.drawable.ic_launcher;
Notification n = new Notification.Builder(ctx)
.setContentTitle(messageTitle)
.setContentText(messageSubtitle)
.setSmallIcon(icon)
.setContentIntent(pIntent)
.setAutoCancel(true).getNotification();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myActivityId, n);
Now when I tap on notification I go to MyActivity activity. I usualy send couple notification at once. When I tap on first notification and then immediately tap on second notification I will have 2 views in my views stack. I mean when I tap back on my phone from second notification activity I'm back to first notification activity. I want to second notification activity replace first notifitaction activity that when I tap back I go to view which was displayed before I started taping on notifications. How can I do that?

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!

Categories

Resources