Android通知声音

我已经使用了较新的NotificationCompat构build器,我无法通知声音。 它会振动并闪光。 Android文档说,设置一个我已经做的风格:

builder.setStyle(new NotificationCompat.InboxStyle()); 

但没有声音?

完整的代码:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notifications Example") .setContentText("This is a test notification"); Intent notificationIntent = new Intent(this, MenuScreen.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); builder.setAutoCancel(true); builder.setLights(Color.BLUE, 500, 500); long[] pattern = {500,500,500,500,500,500,500,500,500}; builder.setVibrate(pattern); builder.setStyle(new NotificationCompat.InboxStyle()); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, builder.build()); 

我之前的代码丢失了什么:

 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound); 

只要把你的声音文件放在Res\raw\siren.mp3文件夹中,然后使用下面的代码:

对于自定义声音:

  Notification notification = builder.build(); notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren); 

对于默认声音:

 notification.defaults |= Notification.DEFAULT_SOUND; 

对于自定义振动:

  long[] vibrate = { 0, 100, 200, 300 }; notification.vibrate = vibrate; 

对于Default Vibrate ::

 notification.defaults |= Notification.DEFAULT_VIBRATE; 

默认声音的另一种方法

 builder.setDefaults(Notification.DEFAULT_SOUND); 

你必须使用RingtoneManager

 private static final int MY_NOTIFICATION_ID = 1; private NotificationManager notificationManager; private Notification myNotification; private final String myBlog = "http://niravranpara.blogspot.com/"; 

带闹钟铃声的noficationmanager代码,您还可以设置铃声RingtoneManager.TYPE_RINGTONE

 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri .parse(myBlog)); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification note = new Notification(R.drawable.ic_launcher, "Alarm", System.currentTimeMillis()); note.setLatestEventInfo(getApplicationContext(), "Alarm", "sound" + " (alarm)", pi); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } note.sound = alarmSound; note.defaults |= Notification.DEFAULT_VIBRATE; note.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(MY_NOTIFICATION_ID, note); 

只要把下面的简单代码:

 notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound_file); 

对于默认声音:

 notification.defaults |= Notification.DEFAULT_SOUND; 

使用可以编码

  String en_alert, th_alert, en_title, th_title, id; int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage; class method Intent intent = new Intent(context, ReserveStatusActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); intent = new Intent(String.valueOf(PushActivity.class)); intent.putExtra("message", MESSAGE); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(PushActivity.class); stackBuilder.addNextIntent(intent); // PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); // android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle(); // bigStyle.bigText((CharSequence) context); notification = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(th_title) .setContentText(th_alert) .setAutoCancel(true) // .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า // .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title)) .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert)) .setContentIntent(pendingIntent) .setNumber(++numMessages) .build(); notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationManager.notify(1000, notification); 

你必须使用生成器。 setSound

 Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); builder.setAutoCancel(true); builder.setLights(Color.BLUE, 500, 500); long[] pattern = {500,500,500,500,500,500,500,500,500}; builder.setVibrate(pattern); builder.setStyle(new NotificationCompat.InboxStyle()); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); builder.setSound(alarmSound); manager.notify(1, builder.build()); 

第一把“yourmp3file”.mp3文件放在原始文件夹(即Res文件夹内)

第二在你的代码放..

 Notification noti = new Notification.Builder(this) .setSound(Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.yourmp3file))//*see note 

这就是我把我的onClick(View v)作为只有“context()。getPackageName()”不会从那里工作,因为它不会得到任何上下文

你可以做一个function:

 public void playNotificationSound() { try { Uri alarmSound = `Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.getInstance().getApplicationContext().getPackageName() + "/raw/notification");` Ringtone r = RingtoneManager.getRingtone(MyApplication.getInstance().getApplicationContext(), alarmSound); r.play(); } catch (Exception e) { e.printStackTrace(); } } 

在收到通知时调用此函数。

这里raw是res中的文件夹,通知是raw文件夹中的声音文件。

 notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);