首页 文章

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

提问于
浏览
53

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

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

这就是我从firebase向我的app Blogpost link发送通知的方法 .

How to Add firebase in your Android Application

8 回答

  • 8

    在通知的通知有效载荷中,存在声音键 .

    从官方文档中可以看出:

    表示设备收到通知时播放的声音 . 支持应用程序中捆绑的声音资源的默认值或文件名 . 声音文件必须位于/ 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会将通知消息定向到系统托盘 . 用户点按通知会默认打开应用启动器 . 这包括包含通知和数据有效负载的消息 . 在这些情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递 .

  • 0

    adavaced options
    选择 advanced optionsWrite a message ,并选择声音 activated
    choose activated

    这是我的解决方案

  • 3

    你可以这样

    {
        "to" : "DEVICE-TOKEN",
    
        "notification" : {
          "body"  : "NOTIFICATION BODY",
          "title" : "NOTIFICATION TITILE",
          "sound" : "default"
        }
      }
    

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

  • 79

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

    来自Firebase docs

    对于下游消息传递,FCM提供两种类型的有效负载:通知和数据 . 对于通知类型,FCM会代表客户端应用自动向最终用户设备显示消息 . 通知具有一组预定义的用户可见键 . 对于数据类型,客户端应用程序负责处理数据消息 . 数据消息只有自定义键值对 . 当您希望FCM处理代表客户端应用程序显示通知时使用通知 . 当您希望应用程序处理显示或处理Android客户端应用程序上的消息时,或者如果您希望在存在直接FCM连接时向iOS设备发送消息,请使用数据消息 .

    再往下docs

    接收包含通知和数据有效负载的消息时的应用程序行为取决于应用程序是在后台还是前台 - 实际上是否在接收时处于活动状态 . 在后台,应用程序在通知托盘中接收通知有效负载,并仅在用户点击通知时处理数据有效负载 . 在前台时,您的应用会收到一个消息对象,其中包含两个可用的有效负载 .

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

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

  • 0

    这样做

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
        //codes..,.,,
    
        Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSound(sound);
    
    }
    
  • 21

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

  • 9

    试试这个....

    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控制台 .

  • 0

    使用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"
           }
        }
      }
    }
    

相关问题