2013-04-04 274 views
124

我已经使用了较新的NotificationCompat构建器,并且无法通知发出声音。它会振动并闪光。 Android文档说设置我已经完成的风格: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()); 
+7

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)也应努力 – Nepster 2016-06-20 09:12:47

回答

219

是什么从我以前的代码丢失:

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

它继续玩,不会停止,我怎么让它只响一次? 使用builder.setOnlyOnce(true);没有帮助 – Salis 2017-05-20 05:23:55

+0

其一次玩 – blackHawk 2017-06-09 10:11:27

+0

builder.setOnlyOnce(true)在我的情况下解决了我的问题! – ElOjcar 2017-07-16 08:09:08

8

你必须使用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); 
+0

抱歉,这不是你的新NotificationCompat.Builder做到这一点。 – 2013-04-06 20:05:16

+0

可以使用NotificationCompat.Builder.build()函数创建通知,并且可以在传递给NotificationManager.notify之前获取build()的返回值并修改其值。这没有多大意义,但完全没问题。 – holgac 2013-06-23 15:08:35

4

你必须使用生成器。 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()); 
123

只要把你的声音文件在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; 

默认振动:

notification.defaults |= Notification.DEFAULT_VIBRATE; 
+0

这对我有用,非常感谢 – 2017-07-22 03:29:30

8

只要把下面简单的代码:

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

对于默认声音:

notification.defaults |= Notification.DEFAULT_SOUND; 
41

另一种方式为默认的声音

builder.setDefaults(Notification.DEFAULT_SOUND); 
1
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); 
9

的使用可以Codeding

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); 
+0

这是唯一一个适合我的人!谢谢! – Michael 2015-09-25 19:21:19

3

1放在原始文件夹(res文件夹内即) “yourmp3file” .MP3文件

在你的代码投放第二..

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

这就是我把我的onClick(视图v)作为唯一的“上下文()。getPackageName()”从那里,因为它不会得到任何上下文

3

,您可以拨打功能无法工作。

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

呼叫时,您会收到通知,此功能

这里生是在res文件夹而该通知是在原文件夹中的声音文件。

+0

最后我结束了使用这段代码,谢谢 – 2017-05-26 10:59:35