Вибрация и звук по умолчанию при уведомлении
Я пытаюсь получить по умолчанию вибрировать и звуковой сигнал, когда приходит мое уведомление, но пока не повезло. Я представляю это как-то связано с тем, как я установил по умолчанию, но я не уверен, как это исправить. Есть мысли?
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);
}
6 ответов:
некоторые фиктивные коды могут помочь вам.
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; }
расширение к ответу TeeTracker,
чтобы получить звук уведомления по умолчанию, вы можете сделать следующее
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);Это даст вам звук уведомления по умолчанию.
уведомления вибросигнал
mBuilder.setVibrate(new long[] { 1000, 1000});звук
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
его работа хорошо для меня, вы можете попробовать его.
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()); }
Это простой способ вызова уведомления с помощью вибрации и звука по умолчанию из системы.
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()); }добавить вибрировать разрешение, если вы собираетесь использовать его:
<uses-permission android:name="android.permission.VIBRATE"/>удачи'.
Я использую следующий код и его работа хорошо для меня .
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."); }
Comments