No notification sound when sending notification from firebase in android

前端 未结 9 1637
迷失自我
迷失自我 2020-12-01 01:00

I am sending push notification from firebase to my Android Application. but when my app is in background firebase onMessageReceived method is not called instead firebase sen

相关标签:
9条回答
  • 2020-12-01 01:32

    Try this

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

    @note for custom notification sound:-> "sound" : "MyCustomeSound.wav"

    0 讨论(0)
  • 2020-12-01 01:32

    I am able to play notification sound even if I send it from firebase console. To do that you just need to add key "sound" with value "default" in advance option.

    0 讨论(0)
  • 2020-12-01 01:33

    The onMessageReceived method is fired only when app is in foreground or the notification payload only contains the data type.

    From the Firebase docs

    For downstream messaging, FCM provides two types of payload: notification and data.

    For notification type, FCM automatically displays the message to end-user devices on behalf of the client app. Notifications have a predefined set of user-visible keys.
    For data type, client app is responsible for processing data messages. Data messages have only custom key-value pairs.

    Use notifications when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app, or if you want to send messages to iOS devices when there is a direct FCM connection.

    Further down the docs

    App behaviour when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.
    When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
    When in the foreground, your app receives a message object with both payloads available.

    If you are using the firebase console to send notifications, the payload will always contain the notification type. You have to use the Firebase API to send the notification with only the data type in the notification payload. That way your app is always notified when a new notification is received and the app can handle the notification payload.

    If you want to play notification sound when app is in background using the conventional method, you need to add the sound parameter to the notification payload.

    0 讨论(0)
  • 2020-12-01 01:40

    select advanced options when Write a message, and choose sound activated

    this is My solution

    0 讨论(0)
  • 2020-12-01 01:40

    With HTTP v1 API it is different

    Documentation

    Example:

    {
     "message":{
        "topic":"news",
        "notification":{
           "body":"Very good news",
           "title":"Good news"
        },
        "android":{
           "notification":{
              "body":"Very good news",
              "title":"Good news",
              "sound":"default"
           }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 01:48

    try this....

      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());
        }
    

    and in onMessageReceived, call it

     @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    
        Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");
    
        buildPushNotification(/*your param*/);
    }
    

    or follow KongJing, Is also correct as he says, but you can use a Firebase Console.

    0 讨论(0)
提交回复
热议问题