Vibration alert and default sound for notification

I try to get a default vibration and sound alert when my notification arrives, but so far no luck. I assume this is due to the way I set the defaults, but I'm not sure how to fix this. Any thoughts?

public void connectedNotify() { Integer mId = 0; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notify) .setContentTitle("Device Connected") .setContentText("Click to monitor"); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); mBuilder.setOngoing(true); Notification note = mBuilder.build(); note.defaults |= Notification.DEFAULT_VIBRATE; note.defaults |= Notification.DEFAULT_SOUND; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(mId, note); } 
+91
android
Aug 15 '13 at 13:17
source share
6 answers

Some dummy codes may help you.

  private static NotificationCompat.Builder buildNotificationCommon(Context _context, .....) { NotificationCompat.Builder builder = new NotificationCompat.Builder(_context) .setWhen(System.currentTimeMillis()).......; //Vibration builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); //LED builder.setLights(Color.RED, 3000, 3000); //Ton builder.setSound(Uri.parse("uri://sadfasdfasdf.mp3")); return builder; } 
+196
Aug 15 '13 at 13:23
source share

TeeTracker answer extension,

to get the default notification sound, which you can do as follows

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notify) .setContentTitle("Device Connected") .setContentText("Click to monitor"); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound); 

This will give you the default notification sound.

+60
Aug 28 '14 at 10:36 on
source share

Vibration Notification

 mBuilder.setVibrate(new long[] { 1000, 1000}); 

Sound

 mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 

for additional sound settings

+34
Mar 25 '15 at 8:45
source share

His work is beautiful for me, you can try.

  protected void displayNotification() { Log.i("Start", "notification"); // Invoking the default notification service // NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setAutoCancel(true); mBuilder.setContentTitle("New Message"); mBuilder.setContentText("You have "+unMber_unRead_sms +" new message."); mBuilder.setTicker("New message from PayMe.."); mBuilder.setSmallIcon(R.drawable.icon2); // Increase notification number every time a new notification arrives // mBuilder.setNumber(unMber_unRead_sms); // Creates an explicit intent for an Activity in your app // Intent resultIntent = new Intent(this, FreesmsLog.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(FreesmsLog.class); // Adds the Intent that starts the Activity to the top of the stack // stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); // mBuilder.setOngoing(true); Notification note = mBuilder.build(); note.defaults |= Notification.DEFAULT_VIBRATE; note.defaults |= Notification.DEFAULT_SOUND; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // notificationID allows you to update the notification later on. // mNotificationManager.notify(notificationID, mBuilder.build()); } 
+13
May 26 '14 at 10:55
source share

This is an easy way to trigger a notification using the default vibration and sound from the system.

 private void sendNotification(String message, String tick, String title, boolean sound, boolean vibrate, int iconID) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Notification notification = new Notification(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); if (sound) { notification.defaults |= Notification.DEFAULT_SOUND; } if (vibrate) { notification.defaults |= Notification.DEFAULT_VIBRATE; } notificationBuilder.setDefaults(notification.defaults); notificationBuilder.setSmallIcon(iconID) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setTicker(tick) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } 

Add vibration resolution if you intend to use it:

 <uses-permission android:name="android.permission.VIBRATE"/> 

Good luck. "

+9
May 05 '16 at 10:37
source share

I use the observation code and its work for me.

 private void sendNotification(String msg) { Log.d(TAG, "Preparing to send notification...: " + msg); mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("GCM Notification") .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); Log.d(TAG, "Notification sent successfully."); } 
+6
Jul 16 '15 at 9:50
source share



All Articles