默认声音的另一种方式
builder.setDefaults(Notification.DEFAULT_SOUND);
第1个将“yourmp3file”.mp3文件放在raw文件夹中(即Res文件夹内)
在您的代码中放置第2 ..
Notification noti = new Notification.Builder(this) .setSound(Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.yourmp3file))//*see note
这就是我放在我的onClick(View v)中,因为只有“context()。getPackageName()”不会在那里工作,因为它不会得到任何上下文
我以前的代码遗漏了什么:
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound);
只需输入以下简单代码:
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound_file);
对于默认声音:
notification.defaults |= Notification.DEFAULT_SOUND;
private void showNotification() { // intent triggered, you can add other intent for other actions Intent i = new Intent(this, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0); //Notification sound try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { e.printStackTrace(); } // this is it, we'll build the notification! // in the addAction method, if you don't want any icon, just set the first param to 0 Notification mNotification = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { mNotification = new Notification.Builder(this) .setContentTitle("Wings-Traccar!") .setContentText("You are punched-in for more than 10hrs!") .setSmallIcon(R.drawable.wingslogo) .setContentIntent(pIntent) .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) .addAction(R.drawable.favicon, "Goto App", pIntent) .build(); } NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // If you want to hide the notification after it was selected, do the code below // myNotification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, mNotification); }
在任何地方调用此函数。这对我有用
使用可以编码
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);
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
通过下面给出的Notification.builder类实例(构建器),您可以在通知时播放默认声音:
您可以执行以下操作:
MediaPlayer mp; mp =MediaPlayer.create(Activity_Order_Visor_Atender.this, R.raw.ok); mp.start();
您可以在资源之间使用raw名称创建一个包,然后保留您的声音,然后调用它。
只需将您的声音文件放入 Res\raw\siren.mp3 文件夹,然后使用此代码:
Res\raw\siren.mp3
对于自定义声音:
Notification notification = builder.build(); notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);
对于Custom Vibrate:
long[] vibrate = { 0, 100, 200, 300 }; notification.vibrate = vibrate;
对于默认振动:
notification.defaults |= Notification.DEFAULT_VIBRATE;
你必须使用建设者。 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());
在Oreo(Android 8)及以上版本中,应该以这种方式完成自定义声音(通知频道):
Uri soundUri = Uri.parse( "android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.push_sound_file); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM) .build(); // Creating Channel NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID", "YOUR_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH); channel.setSound(soundUri, audioAttributes); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)) .createNotificationChannel(notificationChannel);
你可以做一个功能:
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文件夹中的声音文件。