android java notification tap reset activity - android

i use this code for notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
when i tap on notification app open and restart MainActivity i dont want to restart MainActivity.

If you want to just bring your app to the foreground (if it is running) or start it (if it is not running), you should use the following approach:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),
"notify_001");
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
This simulates clicking on the app icon on the HOME screen.

If possible try to add the launch modes to the main activity.
in manifest under the activity tag just use this tag. Or you can add this mode while starting the pending intent for main activity from the notification manager class.
android:launchMode="singleTask"
sample code
<activity
android:name=".MainActivity"
android:label="singleTask"
android:launchMode="singleTask"
android:taskAffinity="">
this should make the new launch to pass the data to existing activity if any opened and is present on back stack.
Also make sure you override the below method to receive the new data in main activity
protected void onNewIntent (Intent intent){
//your update code goes here
}

Related

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.

Open dialog fragment upon click of notification

I receive a notification in MainActivity. When I click on it, it should open the dialog fragment. Currently I am doing this -
String textNotificationMessage = textMessageReceivedEvent.getMessage();
Intent notificationIntent = new Intent(MainActivity.this, MessagingDialogFragment.class);
notificationIntent.putExtra("NotificationMessage",textNotificationMessage);
MessagingDialogFragment messagingDialogFragment = (MessagingDialogFragment) MessagingDialogFragment.instantiate(MainActivity.this, MessagingDialogFragment.class.getName());
messagingDialogFragment.show(getSupportFragmentManager(),MessagingDialogFragment.class.getName());
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
What this does is, whenever I have a notifictaion, it opens the DialogFragment automatically without a click. But I need it to open after a click. How do I achieve this?
Done like this create an activity named MyDialog.java
Now in your manifest file do like this given below
<activity
android:name=". MyDialog"
android:theme="#android:style/Theme.Dialog" />
now navigate to this activity on click event of notification.
The only way to set onClickListener on a notification is through a PendingIntent. Just make the PendingIntent open up one of your Activity and have your Activity be complete transparent and put the code of opening a dialog in onCreate() and finish() the Activity on dismiss of the dialog.
Intent notifyIntent = new Intent(context,ActivityContainingDialog.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Activity gets minimized when launching it from foreground service

I have two Activities in my app:
A - login Activity
B - main Activity
When the user clicks the login button in activity A I am starting a service. In the service's onCreate() method I launch activity B like this:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Activity B in foreground service");
builder.setSmallIcon(R.drawable.ic_launcher);
startForeground(1, builder.build());
The problem is that Activity B launches minimized. I have to press the service button in the notification bar in order to get my activity maximized (fill the screen). How could I launch Activity B from service in normal way - without minimizing it (putting to background)?
Your code doesn't actually launch Activity B at all. All it does is create a Notification, that, if selected by the user, will launch Activity B.
If you want to actually launch Activity B, do this:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Now launch the activity immediately
startActivity(notificationIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
... (rest of your code here)
Question already answered in android start activity from service
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
http://developer.android.com/training/basics/firstapp/starting-activity.html
Finally I found out the solution:
onCreate() method in service class:
#Override
public void onCreate() {
Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getApplication().startActivity(notificationIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Activity B runs in a foreground service");
builder.setSmallIcon(R.drawable.ic_launcher);
startForeground(1, builder.build());
}
In AndroidManifest.xml add this line in Activity element:
android:launchMode="singleTop"

Notification addAction creates button with no behavior

I am trying to add a button to my notification in android.
I use the addAction method in order to add an intent which supposes to open up the main activity (same as clicking the entire notification) but with an extra bundle with data.
this is what I have done so far:
notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
//regular intent to view main activity
PendingIntent contentIntent = PendingIntent.getActivity(this,Constants.MAIN_ACTIVITY,
new Intent(this, MainActivity.class), 0);
//intent for viewing transaction dialog, within main activity using PURCHASE_DIALOG request code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(Constants.LIST, (java.util.ArrayList<? extends android.os.Parcelable>) list);
PendingIntent purchaseIntent = PendingIntent.getActivity(
this, Constants.PURCHASE_DIALOG,
new Intent(this, MainActivity.class), 0, bundle);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(Constants.NOTIFICATION_TOPIC)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(watchResponse.toString()))
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Buy", purchaseIntent)
.setContentText(message);
mBuilder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I simply expect that my onResume in MainActivity class will be called once clicking the extra action, and there i'll be able to get the bundle trough getIntent(), yet nothing happens when I click on it. the button is clicked, but the activity remains open and my application activity wont start.
I had a very similar issue but a very different solution. Pending intent is also not fired if you have declared <service android:enabled="false"></service> in your manifest.xml file.
Replace from android:enabled="false" to android:enabled="true"
This might not be a direct issue of the problem. But if you create the service in android studio using default template it automatically adds these properties to the service.
If this does not work there is a similar question you can find here:
Android Notification Action is not fired (PendingIntent)

Clearing the old activities so they don't show up on new activity click

So what I have is a notification in my status bar which when clicked by the user brings up an activity with no title, to replicate a dialog. But I have a problem. If I open up the app, then just click the home button, and then click the notification in my status bar. It brings up apps main activity with the notificatoins activity stacked on the top. What I want to do is make it so when I click the notification it clears all the bottom activities so that never happens. Here's the code I use to start the notification activity
// Send Notification
Intent intent1 = new Intent(this, Dialog.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_quick_tweet_icon)
.setContentTitle("Dialog")
.setContentText("Touch for more options")
.setWhen(System.currentTimeMillis())
.setContentIntent(pIntent)
.setAutoCancel(false).setOngoing(true);
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.getNotification();
nm.notify(1, notif);
This isn't going to work. You've written:
Intent intent1 = new Intent(this, Dialog.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
What this says is "if activity Dialog already exists in the task stack, clear (finish) all activities that are on top of that in the stack before starting the Dialog activity. Otherwise (if there is no instance of Dialog already in the stack) just start the Dialog activity". So what you are seeing is that your Dialog activity is put on top of the other activities that are already in the task stack.
If you want this notification to remove all activities from the task stack and then start your Dialog activity, I suggest you do the following:
Create the notification like this:
Intent intent1 = new Intent(this, MyRootActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.putExtra("startDialog", true);
in this case, MyRootActivity must be the root activity of the task (ie: the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest).
In onCreate() of MyRootActivity do this:
super.onCreate(...);
if (getIntent().hasExtra("startDialog")) {
// User has selected the notification, so we show the Dialog activity
Intent intent = new Intent(this, Dialog.class);
startActivity(intent);
finish(); // finish this activity
return; // Return here so we don't execute the rest of onCreate()
}
... here is the rest of your onCreate() method...
Hopefully this is clear.

Categories

Resources