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);
Related
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
}
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"
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.
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.
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);