2016-01-14 90 views
0

我有自己的BroadcastReceiver发送推送通知。我需要在应用程序中为通知提供声音。这是我现在的代码:Android通知声音不起作用

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

接收器工作并发送通知,但没有声音。我也尝试过setSound(uri)的NotificationCompat.Builder,它也不起作用。我究竟做错了什么?

回答

-1

更改此LINE-

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

到 -

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

你逝去的旧的通知对象