繁体   English   中英

Android通知声音不起作用

[英]Android notification sound is not working

我有自己的BroadcastReceiver,它可以发送推送通知。 我需要在应用程序中为通知提供声音。 这是我现在拥有的代码:

public class TimeReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Log.d("tag", "onReceive");
      sendNotification(context);;
}

   private void sendNotification(Context context) {
      NotificationCompat.Builder builder = createBuilder(context);
      Notification notification = builder.build();
      notification.defaults |= Notification.DEFAULT_SOUND;
      getNotificationManager(context).notify(1, builder.build());
   }

   private NotificationManager getNotificationManager(Context context) {
      return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   }

   private NotificationCompat.Builder createBuilder(Context context) {
      NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_keyboard_arrow_right_black_24dp)
            .setContentTitle(context.getResources().getString(R.string.title))
            .setContentText(context.getResources().getString(R.string.content))
            .setAutoCancel(true);
      return builder;
   }
}

接收器可以工作并发送通知,但是没有声音。 我也为NotificationCompat.Builder尝试了setSound(uri),它也无法正常工作。 我究竟做错了什么?

更改此行-

getNotificationManager(context).notify(1, builder.build())

至 -

getNotificationManager(context).notify(1, notification). 

您正在传递旧的通知对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM