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);
Related
I'm able to queue and launch notifications through AlarmManager, I'm also able to launch my application when clicking the notification. Unfortunately the notification isn't removing itself when the application is launched.
Notification setup:
Intent intent = new Intent(mainActivity, NotificationPublisher.class);
intent.setAction("handle");
PendingIntent pIntent = PendingIntent.getBroadcast(mainActivity, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(mainActivity)
.setSmallIcon(R.drawable.phone)
.setContentTitle(title)
.setContentText(textContent)
.setAutoCancel(true)
.setContentIntent(pIntent);
return builder.getNotification();
The notification click broadcasts and hits this function:
void handle(Context context, Intent intent) {
System.out.println("handle");
Context mainContext = Extension.mainContext;
Activity mainActivity = Extension.mainActivity;
Intent launchIntent = new Intent(mainContext, mainActivity.getClass());
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launchIntent);
}
The handle function will launch the activity but not remove the notification from the status bar. Interestingly... If I remove the call to startActivity, the notification will close.
Things I've tried:
Setting the auto cancel flag manually
Using the builder.build() instead
NotificationManger cancel(id) and/or cancelAll
Using NotificationCompat.Builder
Using a regular launch intent for the setContentIntent instead of manually calling startActivity
Based on this statement from your question:
Interestingly... If I remove the call to startActivity, the
notification will close.
I'm assuming that your Activity is just reposting the Notification.
I worked around this by launching my App 300ms after clicking the notification.
I am building a mono android app that receives notifications from GCM and opens an activity when then user clicks on the notification.
An issue is occurring when an instance of the activity the notification creates already exists and is the current active activity in the application. Upon clicking the notification a duplicate activity is created in the application. The issue is subtle as the new duplicate activity opens in the foreground and looks identical to previous activity, however when the user clicks the back button the duplicate activity is killed but the previous activity remains meaning the user has to click back button twice.
The following is current code used to generate notification and create an activity on click. I would hope the process would be something like, if activity exists then open existing activity else start new activity. Appreciate any help thank you.
void createNotification(string title, string desc)
{
//Create notification
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
//Create an intent to show ui
var uiIntent = new Intent(this, typeof(Messaging));
//Create the notification
var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title);
notification.Defaults = NotificationDefaults.Sound;
//Auto cancel will remove the notification once the user touches it
notification.Flags = NotificationFlags.AutoCancel;
//Set the notification info
//we use the pending intent, passing our ui intent over which will get called
//when the notification is tapped.
notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));
//Show the notification
notificationManager.Notify(1, notification);
}
Try this
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )
I have a program in which I call a notification. The notification, if you pull it down, launches a new activity.
mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.stat_sys_secure_green;
CharSequence tickerText = "Browser Security Enabled";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Security Vulnerability Detected";
Intent notificationIntent = new Intent(this, PrivacyMessage.class);
//Test Extra
notificationIntent.putExtra("Primary Key", "Primary Text");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
The problem comes later in the code, when I want to refresh the secondary activity. The main activity should be able to dynamically change the extras in it. I tried doing this by launching a new intent.
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Test New Notification";
Intent intent = new Intent(this, PrivacyMessage.class);
notification.icon = R.drawable.stat_sys_secure_orange;
intent.putExtra("Test Thing", "Test Value");
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent cI = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "New Title", "NewText", cI);
mNotificationManager.notify(HELLO_ID, notification);
Now, when I execute that code, the new notification title pops up, the icon color changes, and the pulldown reflects the new title and addition information. However, when I click on it, it does not launch the activity with the new intent. Instead, it just pulls out the old activity with the old extras. I tried both FLAG_ACTIVITY_CLEAR_TOP, and FLAG_ACTIVITY_NEW_TASK, but neither one seems to clear the old secondary activity and create a new one. Any idea on how I might do that?
Apparently this is actually a bug/feature of the android environment. Unless a pendingIntent() is passed with a unique requestCode, it simply retrieves the old intent that was originally passed with that number.
Details can be found here:
http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4
The solution they came up with was to simply increment the requestCode every time pendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) is called, and set the flags the way I had done it originally with
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Which, doesn't seem like a perfect solution, but it works. Thank you guys for your help!
I think, first of all, you should forget about the FLAG_ACTIVITY_NEW_TASK, cause this would open a new task (group of activities) without clearing anything you previously opened. The FLAG_ACTIVITY_CLEAR_TOP wouldn't vbe useful for you either, cause, if I understand the scenario correctly your taks has only two activities, and your target activity is the secondary.
So here it's my question... If the second piece of code is executed within an Activity context, why don't you just call startActivity with the new extras? This would allow you to handle the new extras on the onStart method of the secondary activity.
Regards.
Are you overriding onNewIntent() to catch the new intent, or just calling getIntent()? The onNewIntent() documentation says that getIntent() will continue to return the original intent used to launch the activity, unless you call setIntent() from onNewIntent().
I have created two applications.
One application is message receiver (app1) and another application (app2) is for doing other tasks based on the message.
First application (app1) receives a message, creates the notification and shows up in the top.
When user clicks the notification, it invokes the another application (app2) to do the other tasks based on the message.
If the application (app2) is not running, it should be started. If it is already running, the instance should be displayed and tasks to be done.
I am using following code:
protected void displayNotification() {
Notification notification = new Notification(icon, tickerText, when);
Bundle xtra = new Bundle();
Intent ntent = new Intent(Intent.ACTION_SEND);
ntent.setClassName("com.example.mytestapp",
"com.example.mytestapp.MainActivity");
xtra.putString("id", "8610B0DD");
xtra.putParcelable("message", msg);
ntent.putExtras(xtra);
ntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
ntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);
final int button_Click = 1;
nm.notify(button_Click, notification);
}
This works fine but it creates multiple instances of another application (app2).
Is there any way to prevent creating this multiple copies?
Have you tried setting "singleTask" or "singleInstance" for the launchMode of the activity?
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Works perfect with this code.
Intent ntent = new Intent();
ntent.setClassName("com.project.test",
"com.project.test.MainActivity");
ntent.setType("vnd.android-dir/mms-sms");
ntent.putExtras(bundle);
int flags = Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP;
ntent.setFlags(flags);
//startActivity(ntent);