Notification Sound Stops on Click - android

I use the following code to add the notification, and in the notification I add two features, one is Sound and other is flashing light.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.cutomnotification);
contentView.setTextViewText(R.id.textView1,"Custom");
notification.contentView = contentView;
Intent notificationIntent = new Intent(v.getContext(), MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(v.getContext(), 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.sound=android.provider.Settings.System.DEFAULT_RINGTONE_URI;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags=Notification.FLAG_INSISTENT|Notification.FLAG_SHOW_LIGHTS|Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(HELLO_ID, notification);
In this I also use the custom layout,
Now I have two questions,
1) The sound plays and it stops when user clicks on notification tab,but I want to continue the sound until user press any button on custom layout.
2) I add a button in custom layout now i can implement onClickListener() on it.

Add this code after notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND;

You can create the NotificationCompat.Builder object without using setSound(). This will create a notification without any sound.
notification = mBuilder
.setStyle(notiStyle)
.setSmallIcon(notificationIcon)
.setTicker(title)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.build();

Related

Customize Android Notification layout and click of elements

I want to show image from Url in notification panel for that purpose I am trying to make custom UI with following code but its not working
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
and how I can handle the click of each element like image TextView etc. I haven't found any stuff to handle click of each element.
Use remoteView.setOnClickPendingIntent(viewId, pendingIntent) to give click actions to views in your RemoteViews object. This will allow you to do things like start a Service or send a broadcast (to be received by a BroadcastReceiver).

How can I set delete intent to my code?

I've searched many times but I was not able to find what I actually needed,
There is something on stackoverflow like this and this
but I need to implement my code for api level 8 not 11. So how can I implement it in my code?
My code is here:
....
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t_l); //Full title of the notification in the pull down
CharSequence contentText = "click to see notification"; //Text of the notification in the pull down
//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected
//I don't want to use this here so I'm just creating a blank one
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, notify.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
....
How can I implement this?
I think you need NotificationCompat from the Support library, you can use this code to generate a notification, adapt it as you need
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setDeleteIntent(PendingIntent);
mBuilder.setWhen(when);
Intent notificationIntent = new Intent(context, YOURCLASS.class);
notificationIntent.putExtra("url", url);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, random, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(random, notification);

calling tabhost on notification click

i have tried notification sample.
i can able to open activity on notification click event.
is it possible to open the desired tab widget on notification click event.
any any body help me to open the tab host on notification click event.
please check the below code..
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context, Viewmessage.class);
notificationIntent.putExtra("NotificationMessage",message);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, title, message, pendingNotificationIntent);
notificationManager.notify(0, notification);
Assuming you're using a TabHost -- If you need to set the current tab programmatically you can use TabHost#setCurrentTab(int) or TabHost#setCurrentTabByTag(String)

Notification inbox style

I wanted to create something like an instant messaging application. How do I display multiple messages all in one notification? I can create a notification that appear when user receive a single notification. But when the user receive more than one message how can I update the notification with the previous message? Should I save the messages into a database and display it out if the user did not cancel the notification? Or is there any other way that I can handle this?
Below is my notification code.
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "IMTest- A new event is created" , when);
Intent notificationIntent = new Intent(context, IM_Chat.class);
notificationIntent.putExtra("topicId", topicId);
notificationIntent.putExtra("sender", sender);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 1, notificationIntent, Intent.FLAG_ACTIVITY_MULTIPLE_TASK | PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, topicName, "A new event ["+eventName+"] is added in "+topicName, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.ledARGB |= 0xff0000ff;
notification.ledOffMS |= 1000;
notification.ledOnMS |= 300;
notificationManager.notify(CommunitiesappConstant.NOTIFICATION_ID, notification);
You can update notification using same id and builder
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
private NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
private mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Different Id's will show up as different notifications
private int mNotificationId = 1;
//Some things we only have to set the first time.
private boolean firstTime = true;
private updateNotification(String message, int progress) {
if (firstTime) {
mBuilder.setSmallIcon(R.drawable.icon)
.setContentTitle("My Notification")
.setOnlyAlertOnce(true);
firstTime = false;
}
mBuilder.setContentText(message)
.setProgress(100, progress, true);
mNotificationManager.notify(mNotificationId, mBuilder.build());
}

notification bar - how to write text there

I want to know how to write notification text only on the notification bar and different text on the Notification layout?
The accepted answer contains deprecated class and methods. Look at here.
Here is an example using NotificationCompat.Builder to build Notifications :
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.my_drawable_resource)
.setContentTitle("my title")
.setContentText("my content")
.setContentIntent(pendingIntent);
Notification notification = mBuilder.build();
// default phone settings for notifications
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
// cancel notification after click
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// show scrolling text on status bar when notification arrives
notification.tickerText = title + "\n" + content;
// notifiy the notification using NotificationManager
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
May help newcomers !
The NotificationCompat.Builder class has method setTicker() that allows you to do that.
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Hello World!"; // expanded message text
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
you should look at this link......... Click Me
The first text that appears in the notification bar is defined by the setTicker() method.
The text for the title is defined by setContentTitle().
The text for the content is defined by setContentText().
This is an example:
private void showCustomNotification(){
final int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.mipmap.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, getString(R.string.app_name), when);
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Java (programming language)")
.setTicker("Java (Open to see the info).")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentText("Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,[14] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.");
// NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Categories

Resources