【问题标题】:Android Notification Not getting Sound And VibrationAndroid通知没有声音和振动
【发布时间】:2017-10-05 12:58:06
【问题描述】:

我在我的应用中使用了 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());

提前致谢。

【问题讨论】:

  • 您是否在 android manifest 中为振动添加了权限..?

标签: android firebase push-notification notifications


【解决方案1】:

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

像这样

2. 还要确保您已在 android manifest 中添加了振动权限。

<uses-permission android:name="android.permission.VIBRATE" />

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

通知的通知载荷有一个声音键。

从官方文档看它的用途是:

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

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

    "notification" : {
      "body" : "body",
      "title" : "title",
      "icon" : "myicon",
      "sound" : "default"
    }
  }

【讨论】:

  • 我正在使用 Php 从 Web 服务器发送通知
【解决方案2】:

试试这个

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

【讨论】:

    【解决方案3】:

    IMO 您必须在清单文件中添加权限,如下所示

    <uses-permission android:name="android.permission.VIBRATE" />
    

    【讨论】:

    • 已添加但问题依然存在
    【解决方案4】:

    Android 通知没有声音和振动

    尝试使用默认RingtoneManager

    删除 builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));

    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(uri);
    

    在 android 清单中添加了振动权限。

    <uses-permission android:name="android.permission.VIBRATE" />
    

    重要提示: 确保云服务器端(GCM/Firebase)正确配置 Android 推送通知服务

    确认您正在获取正确的下游负载 (JSON)

     {
          "to": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
          "notification": {
            "body": "great match!",
            "title": "SamDev Test",
            "icon": "myicon",
            "sound": "default"
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多