how to use NOTIFICATION_SERVICE in android - 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);
}

Related

Android Status bar notification

private void triggerNotification(String s)
{
CharSequence title = "TASK";
CharSequence message = s;
notificationManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.vianetlogo, s, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, null, 0);
notification.setLatestEventInfo(c, title, message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
Here it's saving the last notification in status bar if there are mutiple notifications at the same time. Is there any way that it will save mutiple notifications on status bar ?
your answer is here :
If the PendingIntent has the same operation, action, data, categories, components, and flags it will be replaced.
Depending on the situation i usually solve this by providing a unique request code either as static values (0,1,2) or the row id of the data I'm receiving from the DB.

Notification sound in 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.

Android Notification Vibration/LED doesn't work?

Hi I have looked at the documentation about notifications but it is of no help. I followed the advice and applied it to the following class: (the issue is commented)
I wanted to apply a vibration and/LED accompanying the status bar notification( status bar notification does work). When I follow the documentation advice, it states I have to insert :otification.defaults |= Notification.DEFAULT_VIBRATE; But I get an error saying that notification cannot be resolved to a variable and if I change "notification" to note.notification, I don't get any notification at all. The application only runs If I delete the lines I've commented for you. I am not sure where I am going wrong? Thanks.
public class ReminderService extends WakeReminderIntentService {
public ReminderService() {
super("ReminderService");
}
#Override
void doReminderWork(Intent intent) {
Log.d("ReminderService", "Doing work.");
Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
//This is where I'm having problems
**notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;**
note.flags |= Notification.FLAG_AUTO_CANCEL;
Where exactly do you define notification? Your Notification instance is the note object, notificiation is thus undefined.
To solve your problem, just replace all notification reference with note.

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