2014-09-01 108 views
1

我看了幾個關於此主題的問題,似乎都表明,只需在通知構建器中使用.setSound(alarmSound)就會讓手機播放聲音。但是,我正在這樣做,它不起作用。Android通知 - setSound不起作用

我可能錯過了一些東西,但我找不到它是什麼。

這裏是我使用發出通知代碼:

private void newArrivalNotification(String locId, String userId) { 

    ... 

    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent mainActivity = new Intent(this, MainActivity.class); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, locId.hashCode(), 
      mainActivity, 0); 

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

    if(alarmSound == null) { 
     Log.w("GcmIntentService", "alarmSound is null"); 
    } 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle(title) 
       .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(msg)) 
       .setSound(alarmSound, AudioManager.STREAM_NOTIFICATION) 
       .setTicker(msg) 
       .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 

    Notification arrivalNotification = mBuilder.build(); 
    arrivalNotification.defaults |= Notification.DEFAULT_VIBRATE; 

    mNotificationManager.notify(NOTIFICATION_ID, arrivalNotification); 
} 

你能找出什麼我做錯了什麼?我找了幾個小時,找不到任何東西。

回答

0

問題是,您正在修改默認通知行爲(聲音和振動)與這行代碼。

Notification arrivalNotification = mBuilder.build(); 
arrivalNotification.defaults |= Notification.DEFAULT_VIBRATE; 
mNotificationManager.notify(NOTIFICATION_ID, arrivalNotification); 

您可以用再次激活:

int defaults = 0; 
defaults |= Notification.DEFAULT_SOUND; 
defaults |= Notification.DEFAULT_VIBRATE; 
mBuilder.setDefaults(defaults); 
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

我希望這可以幫助您! ;)

+0

此外,爲了產生振動,而不是使用'arrivalNotification.defaults | = Notification.DEFAULT_VIBRATE;'你也可以使用你自己的模式。像'long pattern [] = {0,2000, 500,2000};'你可以使用'.setVibrate(pattern)'設置振動,它應該可以正常工作。 – 2014-09-01 23:37:52