Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my code - Notification - no sound no vibrate

I seem to have a problem with my code. I have created a test activity, just to see what's wrong, still I can't.

public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String extra = "test";

    NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, test.class);

    Notification notification = new Notification(R.drawable.icon, 
            extra, 
            System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent);
    notification.flags |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.DEFAULT_VIBRATE;
    notification.flags |= Notification.FLAG_INSISTENT;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    myNotificationManager.notify(33, notification);

}
}

I get no sound and/or vibrate when the notification pops.

I looked at the settings of my phone, and they are ok, no silent, default sound - enabled.

like image 351
Saariko Avatar asked Jul 07 '11 19:07

Saariko


People also ask

Why are my notifications not making Sound?

You might have accidentally enabled Mute or Vibration mode on your Samsung Galaxy phone and that's why you don't hear notification sounds. To disable those modes, you need to enable Sound mode. For that, go to Settings > Sounds and vibration. Check the box under Sound.


2 Answers

This...

notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.DEFAULT_VIBRATE;

should be...

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
like image 122
Will Tate Avatar answered Sep 23 '22 18:09

Will Tate


For all default values (Sound, Vibrate & Light) in 1 line of code you can use:

notification.defaults = Notification.DEFAULT_ALL;

this is the equivalent of

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;

make sure you set permissions for vibrate in your Manifest

<uses-permission android:name="android.permission.VIBRATE" />
like image 40
Adam Avatar answered Sep 24 '22 18:09

Adam