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.
Related
I'm working whit push notifications.
When a user clicks on a notification I want to start the application in case it is not started, or bring it to front in case it is started.
Thanks
this is the complite code I found the answer here Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
The important things are the flags on the Intent, this will open the app or bring it to front if is opened, or do nothing if you click on the notification while you are browsing the app
implement pending intent.
Code
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
manifest
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
Just set the intent for the notification, this is covered in details in the official API guide.
You do that by creating a PendingIntent.
For example, this will launch a MainActivity when notification is clicked.
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
I don't know whether you're talking about Google Cloud Messaging or not. But if it is a normal Notification then while creating your notification you've to provide Pending Intent. Just put your desired class in Pending intent so that when user clicks on that notification you'll be driven to your so called Application. A snippet :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
After this use this intent while creating notification. It's method is setContentIntent("above intent") and fire your notification with NotificationManager!
I have created the notification object using NotificationCompat.Builder and then elsewhere in my service I'd like to update value. What is the right approach to do this? Should I cancel this object and bulid another one?
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText("estimated value:" + String.valueOf(value)) // <---- wanna update this
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.build();
startForeground(1235, noti);
You do not need to cancel your previous notification. Instead, you can simply run the identical code with different content text (in this case, changing value).
For more info, see the Updating Notifications section of the Android docs.
I currently have an ongoing notification that displays some information about my app. While this information is useful, there's no need for the user to actually see that I've created the notification. Its basically there to tell them that "hey, my app is doing stuff in the background, click here to go back to it".
My notification works correctly, however whenever I call ".notify()" my notification shows the text from my notification in the notification bar at the top of the screen, like a text message does.
I want to to do it silently, so when I call notify it doesn't show my notifications text in the small notification bar before doing the "roll up" animation.
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, AudioPlayer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification(R.drawable.ic_launcher,
title, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.note_layout);
~~~ remote views stuff ~~~
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, author);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(8675309, notification);
I'm not sure that the OS allow that.
What you want to hide is called Ticker.
You can try to set an empty Ticker Text:
Notification notification = new Notification(R.drawable.ic_launcher,
"", System.currentTimeMillis());
I am trying to create a custom notification with the help of remote view with a button on it. I want to write onclick listener for this button but the problem is that the notification container is catching the touch event and button is not being clicked. My question is Is there any way to disable this notification and let the button to be clicked. My code is something like below:
Notification notification = new Notification(R.drawable.applogo,
"Apps activated", System.currentTimeMillis());
Intent notificationIntent = new Intent(getBaseContext(), AppsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),
0, notificationIntent , 0);
notification.contentIntent = pendingIntent;
//Remoteview and intent for my button
RemoteViews remoteView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.customnotification);
Intent mIntent = getPackageManager().getLaunchIntentForPackage(c.getString(1));//starting some application through package
PendingIntent pintent = PendingIntent.getActivity(this, 0,
mIntent, 0);
remoteView.setOnClickPendingIntent(R.id.button1,
pintent);
notification.contentView = remoteView;
notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
I am working on api 7.
Thanks
Unfortunately, clickable buttons within notification layout available only with Custom notifications for API >= 11.
Also, for API >= 16 you can use Notification Actions to provide buttons within notification.
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);