Notification sound in android - android

In my android application, I want to notify the user with a sound without showing the notification. Is that possible ?
Notification n = new Notification();
n.icon = com.android.mms.R.drawable.wap_message;
n.defaults |= Notification.DEFAULT_SOUND;
n.flags = Notification.FLAG_AUTO_CANCEL;
This is the code I am using to show the notification, So I commented icon and flags of the notification as follows
Notification n = new Notification();
//n.icon = com.android.mms.R.drawable.wap_message;
n.defaults |= Notification.DEFAULT_SOUND;
//n.flags = Notification.FLAG_AUTO_CANCEL;
But it is not working.

why don't you use MediaPlayer instead of Notification to play the notification sound. If you use the Notication then it shows the icon. In your case using the MediaPlayer is better option.

Related

Can't dismiss notification by swiping

I have a Service that send notification to user acording to some conditions
private void sendNotif(String title, String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this)
.setContentTitle(title)
.setContentText(msg)
.setSmallIcon(android.R.drawable.stat_notify_chat);
Notification notification = builder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
startForeground(DEFAULT_NOTIFICATION_ID, notification);
}
And now I have problems with dismissing this notification.
I try to use: .setOngoing(true);,
I try to use: notification.flags |= Notification.FLAG_ONGOING_EVENT;
Try to use NotificationManager, but dont find way how to startForeground using notification with NotificationManager.
I know that notification is alive while startForeground, so I try to add stopForeground to onDestroy(), but It still didn't help. I google tens of pages, but still can't find way to solve this problem. Will appreciate any help.

Android 4: can't dismiss notification by swiping

I have some code that creates some notifications, it's really basic.
int icon = R.drawable.notification;
CharSequence tickerText = "Text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Text";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, RequestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notificationID, notification);
It all works fine in 2.1.
In 4.0, it all works fine except the swipe-to-dismiss action doesn't work. The notification goes slightly to the side then sticks and bounces back.
Any idea?
Thanks.
You can't swipe away your notification, because it is in an "ONGOING"-State.
First the solution:
Replace setting flags with following code:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Defaults are for the defaults-section, flags for the flags-section.
And now the reason why it was ongoing?
As you might already know flags (and defaults) for notifications are set by a bitwise operation. Means each flag has a constant value which is a power of 2. Adding them results in an unique number for a set of flags which makes it real fast to calculate which flags are actually set.
Notification.DEFAULT_VIBRATE and Notification.FLAG_ONGOING_EVENT
have the same contant value of 2.
You should use setOngoing (boolean ongoing)
Set whether this is an "ongoing" notification. Ongoing notifications
cannot be dismissed by the user, so your application or service must
take care of canceling them. They are typically used to indicate a
background task that the user is actively engaged with (e.g., playing
music) or is pending in some way and therefore occupying the device
(e.g., a file download, sync operation, active network connection).
You can use
.setOngoing(false);
Just insert this line when you make the notification...
// Will show lights and make the notification disappear when the presses it
notification.flags == Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

how to use NOTIFICATION_SERVICE in android

I want to show a notification after receiving a WAP push message in my phone.I know that NOTIFICATION_SERVICE is i need to be used. But i don't know how to use this. Please help me to overcome this problem. I am able to receive the message in my phone and it is printing in the log correctly. Written some code for showing the notification but it is not working. Attaching the code along with please have a look on it and expecting a proper guiding on this.
private void showNotification(String text,Context con) {
Notification n = new Notification();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
n.icon = R.drawable.ic_sms_wap;
//n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(NOTIF_CONNECTED, n);
}
hard to answer without knowing exactly what your problem is (ie do you get an error etc..) but you could try this:
private void showNotification(String text,Context con) {
if (mNotifMan==null) {
mNotifMan=(NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);
}
Notification n = new Notification(R.drawable.ic_sms_wap,text,System.currentTimeMillis());
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
// n.icon = R.drawable.ic_sms_wap;
// n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(null,NOTIF_CONNECTED, n);
}

Android Notifications not disappearing (Even with Auto_Cancel)

I have different notifications that each have a different bundle/activity associated with them. My problem is that they don't disappear once clicked. They aren't under ongoing notifications though and "clear" gets rid of them. Bellow is my code. Any thoughts would be greatly appreciated. :)
private void showNotification(Bundle b){
CharSequence myText = b.getString("notifStr");
Notification notification = new Notification(R.drawable.stat_sample, myText,System.currentTimeMillis());
Intent i = new Intent(myContext, NewPlace.class);
i.setAction(Intent.ACTION_VIEW + Integer.toString(b.getInt("id")));
i.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, i, 0);
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(myContext, myText,myText, contentIntent);
notifMan.notify(b.getInt("id"), notification);
}
try changing:
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
to
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Notification Documentation (Flags)
public int defaults
Since: API Level 1 Specifies which
values should be taken from the
defaults. To set, OR the desired from
DEFAULT_SOUND, DEFAULT_VIBRATE,
DEFAULT_LIGHTS. For all default
values, use DEFAULT_ALL.
You should try
notification.flags |= Notification.FLAG_AUTO_CANCEL;

How can I enable vibration and lights using the Android notifications api?

I have created an application that creates notifications, using the following code:
// notification
Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// parameters
String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
if (ringtone.length() > 0) {
notification.sound = Uri.parse(ringtone);
notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
}
boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
if (useVibrator) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
if (useLed) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
// alert
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, text);
notification.contentView = contentView;
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(1, notification);
The notification works, and the correct ringtone is used.
However, even though the preferences are correctly activated and notification flags are correctly set (I checked by debugging), the notification never vibrates and never cause the lights to be activated.
I would have blamed my phone's settings, but every other app using notifications, like messaging, gmail, and others correctly use all these features.
May someone know what I did wrong ? (my phone is a HTC Hero with Android 2.1)
Add permission to your manifest file
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
EDIT
For Lights try adding them explicitly, the Default light might be configured to be nolight
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
In addition to Pentium10'S answer:
Put your device to sleep and the lights will go on! ;)

Categories

Resources