从android中的firebase发送通知时没有通知声音

时间:2016-06-22 05:52:02

标签: android push-notification notifications firebase firebase-cloud-messaging

我正在从firebase向我的Android应用程序发送推送通知。但是当我的应用程序处于后台时,不会调用firebase onMessageReceived方法而是调用firebase向系统发送通知,以便在系统托盘中显示通知。 通知出现在系统托盘中但没有通知声音,即使我已在系统设置中允许我的应用程序发出通知声音。

当从firebase收到通知时我可以做什么来播放通知声音。

这是我从firebase向我的应用Blogpost link发送通知的方式。

How to Add firebase in your Android Application

10 个答案:

答案 0 :(得分:84)

在通知的通知有效负载中有一个声音密钥。

从官方文档中可以看出:

  

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

例如:

{
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",

    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon",
      "sound" : "mySound"
    }
  }

如果您想使用设备的默认声音,则应使用:"sound": "default"

请参阅此链接以了解有效负载中的所有可能密钥: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

对于那些不知道firebase在应用程序处于后台时处理通知的人。在这种情况下,不会调用onMessageReceived函数。

  

当您的应用在后台时,Android会指示通知   消息到系统托盘。用户点击通知即可打开   应用程序启动器默认情况下。这包括包含两者的消息   通知和数据有效负载。在这些情况下,通知是   传送到设备的系统托盘,数据有效负载是   在您的启动器活动的附加内容中提供。

答案 1 :(得分:22)

adavaced options 撰写邮件时选择高级选项,然后选择声音已激活 choose activated

这是我的解决方案

答案 2 :(得分:11)

试试这个

{
    "to" : "DEVICE-TOKEN",

    "notification" : {
      "body"  : "NOTIFICATION BODY",
      "title" : "NOTIFICATION TITILE",
      "sound" : "default"
    }
  }

自定义通知声音的@note: - > "sound" : "MyCustomeSound.wav"

答案 3 :(得分:9)

仅当app位于前台或通知有效内容仅包含数据类型时才会触发onMessageReceived方法。

来自Firebase docs

  

对于下游消息传递,FCM提供两种类型的有效负载:   通知数据

     

对于通知类型,FCM会自动显示消息   代表客户端应用程序的最终用户设备。通知有一个   预定义的一组用户可见键   对于数据类型,客户端应用程序负责处理数据消息。数据消息只有自定义键值对。

      使用通知如果希望 FCM处理显示   代表您的客户端应用程序发送通知。当您使用使用数据消息时   希望您的应用处理显示或处理您的消息   Android客户端应用,或者如果要向iOS设备发送消息   当有直接的FCM连接时。

再往下docs

  接收包含两个通知的邮件时,

应用行为   和数据有效负载取决于应用程序是在后台还是   前景 - 基本上,它是否在当时是活跃的   收据。
  在后台时,应用会收到通知有效内容   在通知托盘中,只处理数据有效负载时   用户点击通知。
  在前台时,您的应用会收到一个消息对象,其中包含两个有效负载。

如果您使用firebase控制台发送通知,则有效内容将始终包含通知类型。您必须使用Firebase API仅在通知有效内容中发送包含数据类型的通知。这样,当收到新通知并且应用可以处理通知有效负载时,您的应用始终会收到通知。

如果您想使用传统方法在应用程序处于后台时播放通知声音,则需要add the sound parameter通知有效负载。

答案 4 :(得分:7)

我也遇到了必须发出声音的通知的问题,当应用程序处于前台时,一切正常运行,但是当应用程序处于后台时,声音却没有发出。

通知是由服务器通过FCM发送的,也就是说,服务器安装了通知的JSON并将其发送到FCM,FCM然后将通知发送到应用程序。即使贴上了声音标签,声音也不会在背景中发出。

enter image description here

即使放置声音标签也不起作用。

enter image description here

经过如此多的搜索,我在github论坛上找到了解决方案。然后我注意到我的情况有两个问题:

1-发送channel_id标记丢失,这对于在API级别26+中工作很重要

enter image description here

2-在Android应用程序中,对于这种直接从服务器发送通知的特定情况,我必须提前配置频道ID,因此在我的主活动中,我必须配置频道,以便Android知道通知到达时要做的事情。

在服务器发送的JSON中:

   {
  "title": string,
  "body": string,
  "icon": string,
  "color": string,
  "sound": mysound,

  "channel_id": videocall,
  //More stuff here ...
 }

在您的主要活动中:

 @Background
    void createChannel(){
            Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.app_note_call);
            NotificationChannel mChannel;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mChannel = new NotificationChannel("videocall", "VIDEO CALL", NotificationManager.IMPORTANCE_HIGH);
                mChannel.setLightColor(Color.GRAY);
                mChannel.enableLights(true);
                mChannel.setDescription("VIDEO CALL");
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();
                mChannel.setSound(sound, audioAttributes);

                NotificationManager notificationManager =
                        (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(mChannel);
            }
    }

这终于解决了我的问题,希望它可以帮助某人不要像我一样浪费2天。我不知道我在代码中放入的所有内容是否必要,但这就是方法。我也再也找不到github论坛链接来记下答案了,因为我所做的与那里发布的内容相同。

答案 5 :(得分:3)

喜欢这个

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    //codes..,.,,

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

}

答案 6 :(得分:3)

使用HTTP v1 API,它是不同的

Documentation

示例:

{
 "message":{
    "topic":"news",
    "notification":{
       "body":"Very good news",
       "title":"Good news"
    },
    "android":{
       "notification":{
          "body":"Very good news",
          "title":"Good news",
          "sound":"default"
       }
    }
  }
}

答案 7 :(得分:1)

试试这个......

  public  void buildPushNotification(Context ctx, String content, int icon, CharSequence text, boolean silent) {
    Intent intent = new Intent(ctx, Activity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1410, intent, PendingIntent.FLAG_ONE_SHOT);

    Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), //large drawable);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(icon)
            .setLargeIcon(bm)
            .setContentTitle(content)
            .setContentText(text)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    if(!silent)
       notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

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

        notificationManager.notify(1410, notificationBuilder.build());
    }
  

并在onMessageReceived中,将其命名为

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");

    buildPushNotification(/*your param*/);
}

或关注KongJing,正如他所说的那样也是正确的,但您可以使用Firebase控制台。

答案 8 :(得分:0)

即使我从firebase控制台发送声音,我也可以播放通知声音。要做到这一点,您只需要在事先选项中添加值“default”的关键“声音”。

答案 9 :(得分:0)

您需要确保您的系统为您的应用启用声音通知,某些设备(例如小米)默认禁用此选项,因此您在转到以下位置之前不会听到通知声音:

设置 > 通知 >(搜索您的应用并点击它的名称)...

然后启用(Allow Sound)选项如图:

enter image description here

相关问题