Passing data to the application using Notification - android

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

Related

My service resets internal variables when opening ongoing notificiation

I have a setup where my activity starts a service and sends values through the intent that control how the service acts. These variables can change through the life of the service, and the service pushes updates back to the activity UI. If I exit the activity and relaunch it, it will request an update from the service to get the current values. This works fine if I re-launch the activity via the home screen, or if I use the recent apps menu. However if I launch the activity via the run in foreground ongoing notification I have set for the service, then the service re-sets itself to the original values when it was started.
Here's my run in foreground code, I don't think there is anything else that would have an affect on this problem.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);
Update 3:
Adding the flags Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP, and Intent.FLAG_ACTIVITY_SINGLE_TOP to the notification intent fixed the problem. I have no idea why these fixed it. If anyone can tell me why this happens please do. Here's the new code that works.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);

Bringing an Activity to Front , using notification in Android

I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it .
For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .
This is my code -
int icon = R.drawable.android;
long when = System.currentTimeMillis();
CharSequence text = "new message";
CharSequence contentTitle = stanza.getFrom(); // message title
CharSequence contentText = stanza.getBody(); // message text
Intent notificationIntent = new Intent(context, ChatBox.class);
notificationIntent.putExtra("buddyid",stanza.getFrom());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
Have u tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your notificationIntent.addFlag();
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.

Android Refresh Activity from Notification

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().

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

Android notification manager help

I m currently looking into android notification. Is there any way to show all the notification from an application as a single one with the number in android notifications window. Clicking on this will redirect the user to a view in the application with separate notifications. Also Is there any way to set the activity for each notifications so that the user will be redirected to the appropriate activity based on the notification clicked.
You could attach a custom layout to your notification, which contains a counter. And when event occurs increase the counter and update the notification. Similar like in example below:
Notification notification = new Notification(R.drawable.logo, name, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(appContext.getPackageName(), R.layout.custom_layout );
contentView.setTextViewText(R.id.notification_text, Counter);
notification.contentView = contentView;
To attach an activity to the notification:
Intent notificationIntent = new Intent(appContext, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(...);
notificationIntent.putExtras( bundle );
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent contentIntent = PendingIntent.getActivity( appContext, (int)System.currentTimeMillis(), notificationIntent, 0 );
notification.contentIntent = contentIntent;
Hope it will help.

Categories

Resources