Android通知无法获得声音和振动

Gur*_*ran 5 notifications android push-notification firebase

我在我的应用程序中使用了FCM通知,我正在接收它们,它正在显示标题和消息

但是,当我收到通知时,我没有任何声音或振动或任何Led灯指示

我的通知生成器代码是

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.drawable.applogo);
    builder.setContentTitle(Title);
    builder.setContentText(Message);
    builder.setAutoCancel(true);
    builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));
    builder.setContentIntent(pendingIntent);
    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setLights(Color.RED, 1000, 300);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, builder.build());
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Nil*_*hod 5

1.当您发送推送通知时,请firebase确保启用声音选项

像这样 在此输入图像描述

2. 还要确保您在Android清单中添加了振动权限.

<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)

3. 如果从服务器发送而不是使用有效负载

通知的通知有效载荷有一个声音键.

从官方文档中可以看出:

表示设备收到通知时播放的声音.支持默认或应用程序中捆绑的声音资源的文件名.声音文件必须位于/ res/raw /中.

{
    "to" : ".......",

    "notification" : {
      "body" : "body",
      "title" : "title",
      "icon" : "myicon",
      "sound" : "default"
    }
  }
Run Code Online (Sandbox Code Playgroud)