2013-04-04 339 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