android推送通知声音

时间:2018-06-12 01:52:16

标签: android push-notification notifications push

我有一个小型的android项目 这是我的通知代码

public class MyfirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("FCM NOTIFICATION");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
 }
}

但是当通知到来时没有声音 任何帮助,谢谢

1 个答案:

答案 0 :(得分:1)

添加此代码

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysound);
notificationBuilder.setSound(sound);

并将notifysound放入原始文件夹。