Android Notification Vibration/LED doesn't work? - android

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.

Related

Displaying multiple notifications on status bar

I am developing an application. I want to display multiple notification to status bar area. But every time it's showing previous message. I have tried many things but its not working, how can I display multiple notification in an Android status bar.
Below is my code :
private void Notify(String Title, String Message)
{
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,Title, System.currentTimeMillis());
Log.e("NotificationManager","notify");
Intent notificationIntent = new Intent(getBaseContext(), NoteEdit.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Log.e("NotificationManager","notificationIntent");
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getBaseContext(), Title,Message, pendingIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID , notification);
}
}
use this at place of 0 you will have to use a variable and increment it every time
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(),
index++, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Make Notification cleared by user or once clicked

I have a service that may show a notification, the problem is once the notification is set, it doesn't clear neither when clicked, nor when swiped on. I am using the flag Notification.FLAG_AUTO_CANCEL but it doesn't seem to do anything..
private NotificationManager nm;
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private final int NOTIFY_ID = 1;
private void showNotification(String date, String name) {
try{
CharSequence text = "You have new Event";
Notification notification = new Notification(R.drawable.small_icon, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, viewEvents.class).putExtra("date", date), 0);
notification.setLatestEventInfo(this, name, "Date: "+date, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ID, notification);
}catch(Exception e){
Log.i("Notification",e.toString());
}
}
So what am I doing wrong?
Try using Notification.Builder or NotificationCompat.Builder instead of rolling the Notification manually.
Among other things, this would prevent the bug in your code, where you are applying FLAG_AUTO_CANCEL to defaults rather than flags.
Notification.FLAG_AUTO_CANCEL is commented out in your code.
But in case it isn't then flag Notification.DEFAULT_LIGHTS should be setted in notification.defaults not in notification.flags
I use this code to show/hide notifications:
private Handler handler = new Handler();
private Runnable task = new Runnable() {
#Override
public void run() {
NotificationManager manager = (NotificationManager) contesto.getSystemService(Context.NOTIFICATION_SERVICE);
Intent launchIntent = new Intent(contesto.getApplicationContext(), SearchHistory.class);
PendingIntent contentIntent = PendingIntent.getActivity(contesto.getApplicationContext(), 0, launchIntent, 0);
//Create notification with the time it was fired
NotificationCompat.Builder builder = new NotificationCompat.Builder(contesto);
builder.setSmallIcon(R.drawable.ic_launcher).setTicker("MESSAGE HERE!").setWhen(System.currentTimeMillis()).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setContentTitle("TITLE").setContentText("MESSAGE").setContentIntent(contentIntent);
Notification note = builder.build();
manager.notify(100, note);
}
};
And to invoke just put:
handler.post(task);

Android repeated ringing with Notification manager

public class test extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String extra = "test";
NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, another.class);
Notification notification = new Notification(R.drawable.ic_launcher, extra,
System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent);
notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotificationManager.notify(1, notification);
}
}
This snippet code is working fine, the thing is that, when I run it, the notification sound is ringing constantly until i check it on the status bar.
Is there a way to make it ring only one time, instead of constantly ringing ?
Remove the flag Notification.FLAG_INSISTENT
From docs: "Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened."

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