振动和声音默认通知

当我收到通知时,我正试图获得默认的振动和声音警报,但到目前为止还没有运气。 我想这是我设置默认的方式,但我不确定如何解决它。 有什么想法吗?

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); } 

一些虚拟代码可能会帮助你。

  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"/> 

祝你好运,'。

我使用followung代码和它的工作正常。

 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."); }