我有自己的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),但它也没用。我做错了什么?
答案 0 :(得分:0)
更改此行 -
getNotificationManager(context).notify(1, builder.build())
到 -
getNotificationManager(context).notify(1, notification).
您正在传递旧的通知对象