Android Notification Action - android

I try to use local notification and it's actions. I want to create a notification and handle multiple action types. My notification asks a question to the user. There are two options, yes or no. My implementation is below:
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
It recreates the activity. But old activity already alive. When I press back button, I can see it. How can I replace the new activity?

You can use "finish()" to close down the activity before you move onto the next one.
Here is a simple example:
startActivity(intent); <- here I am telling the program to start the desired actitvity
finish(); <- Here I am asking the program to close the current activity before I move onto the next one.
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
finish();

I solve this problem with below line
android:launchMode="singleTop"

Related

How to stop or exit from the app, when user click on notification?

I want to stop or exit from my application when the user press the notification icon, here is my code. with this code i can able to open my main activity by pressing notification icon. all i want is someone to change my existing code, plz help me
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic);
builder.setContentTitle("RadioPlanet");
builder.setContentText("Touch here to stop");int mll=001;
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
builder.setContentIntent(contentIntent);
NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotification.notify(mll,builder.build());
my package name is com.hackerinside.jaisonjoseph.radioplanet
Not too familiar with notifications, but here is a suggestion:
Create a new activity. For this example, I will call it MainActivityStop. From the notification, send a PendingIntent to MainActivityStop. In MainActivityStop, create a new intent and putExtra. EXample:
Intent i = new Intent(MainActivityStop.this, MainActivity.class);
i.putExtra("stop", true);
startActivity(i);
To receive it:
boolean stop = false;
try{
stop = getIntent().getBooleanExtra("stop", false);
}catch(Exception e){
//ignore this. If it gets here, the boolean cannot be found. It may never go to the catch block, it is a safety precaution
}
(...) Do whatever you do to stop or exit.

Set different activities for pending intent of notification

I am currently facing the problem of setting pending action for two different activities to notification.
I have a ParentActivity and a ChildActivity. I want open ChildActivity on notification click if currently it is running or paused, otherwise start ParentActivity.
I tried this :
.........
Intent resultIntent = new Intent(this, ChildActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ParentActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
.............
Above is not working for me. Everytime ChildActivity is starting on notification click.
And also as Faruk answered, I dont want this. Creating a notification's pending intent by checking ChildActivity's current state will not work.
Suppose notification created when ChildActivity was running but after creating the notification, user killed the app. So after killing the app, If user will click on notification then ChildActivity will start. I don't want that. I want if ChildActivity is not running or paused then ParentActivity should be started.
How can I achieve this?
Please help.
While there may be several ways to achieve this, following is the one I can think of.
First, you should get whether ChildActivity is active or not, through this link
Check whether activity is active
Store this in some variable childActive, then you can initialize different notificationIntents checking the value without using task TaskStackBuilder.
For example;
Intent notificationIntent = null;
if(childActive)
notificationIntent = new Intent(context, ChildActivity.class);
else
notificationIntent = new Intent(context, ParentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Have your Notification launch a simple dispatch Activity. This Activity does the following in onCreate():
super.onCreate(...);
if (ChildActivity.running) {
// ChildActivity is running, so redirect to it
Intent childIntent = new Intent(this, ChildActivity.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
childIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(childIntent);
} else {
// Child is not running, so redirect to parent
Intent parentIntent = new Intent(this, ParentIntent.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(parentIntent);
}
finish();
In ChildActivity do this:
public static boolean running; // Set when this Activity is active
In ChildActivity.onCreate() add this:
running = true;
In ChildActivity.onDestroy() add this:
running = false;

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);

Save to DB after clear the push notification android

I am working with push notification and I have one problem.
When I click in the received notification my code launch a new intent with an activity.
I use:
Intent i = new Intent(getApplicationContext(), DemoActivity.class);
i.putExtra("msg",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
ITS OK FOR ME
But if I want only save this notification in SQLite how can I pass the message?
I know that with:
Intent bd = new Intent(getApplicationContext(), AbaseDatos.class);
bd.putExtra("msg",msg);
PendingIntent contentIntentbd = PendingIntent.getActivity(this, 0,
bd, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setDeleteIntent(contentIntentbd);
Call a new activity when clear the notification, but the new activity open the layout.
I dont want any new activity be launched. Its possible launch a javaclass with an Intent?
Any idea?
You can launch a service with an intent.
http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context%2C%20int%2C%20android.content.Intent%2C%20int)

Android PendingIntent take you to an already existing activity?

Wasn't really sure how to search for this...
I have a the following which is called whenever a job is added or removed from my queue to put a notification in the status bar:
private void showNotification()
{
int jobsize = mJobQueue.size();
int icon = (jobsize == 0) ?
android.R.drawable.stat_sys_upload_done :
android.R.drawable.stat_sys_upload;
Notification notification =
new Notification(icon, "Test", System.currentTimeMillis());
Intent intent = new Intent(this, FileManagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags =
(Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(this,
"Uploading to our servers",
getString((jobsize > 0) ?
R.string.notification_active_transfers :
R.string.notification_no_transfers),
pendingIntent);
mNotifyManager.notify(NOTIFICATION, notification);
}
As it is now the behavior is this:
if the user logs out and hits the notification, it will still open a new FileManagerActivity (ops!) I could get around this by starting at my authentication activity and passing the intent up my stack in a natural order, its when the application is already running is where I have difficulties.
if the user already has the FileManagerActivity open clicking the notification will put a second instance over it. In this case, I want the currently running FileManagerActivity to recieve focus instead of launching a new instance.
How could I get the correct behavior?
I've done this before by setting my Activity to use the launch mode 'singleTop' in the Application Manifest. It will achieve the desired function, using the existing activity if one exists. In this case, onNewIntent will be called in your activity.
You'll need to check in your FileManagerActivity for authentication and start a new activity as appropriate if the user is not logged in.
I think Worked when added these:
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent.FLAG_UPDATE_CUR
Intent intent = new Intent(context, MyOwnActivity.class);
intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

Categories

Resources