capawesome-team / capacitor-firebase

⚡️ Firebase plugins for Capacitor. Supports Android, iOS and the Web.

Home Page:https://capawesome.io/plugins/firebase/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: Issue with presentationOptions Config Affecting Background Notification Sound

sinanverve7 opened this issue · comments

Plugin(s)

  • Analytics
  • App
  • App Check
  • Authentication
  • Crashlytics
  • Cloud Firestore
  • Cloud Messaging
  • Cloud Storage
  • Performance
  • Remote Config

Version

5.4.1

Platform(s)

  • Android
  • iOS
  • Web

Current behavior

When configuring FirebaseMessaging's presentationOptions in my Capacitor app:

  • Setting presentationOptions to an empty array ([]) suppresses sound for both foreground and background notifications, leading to silent background notifications, which is not desired.

FirebaseMessaging: { presentationOptions: [], },

  • Conversely, including "sound" in presentationOptions (["sound"]) activates sound for both foreground and background notifications. This is problematic as it doesn't allow for silent handling of foreground notifications while ensuring background notifications are audible.

Conversely, including "sound" in presentationOptions (["sound"]) activates sound for both foreground and background notifications. This is problematic as it doesn't allow for silent handling of foreground notifications while ensuring background notifications are audible.

This configuration behavior does not allow for differentiated handling of notification sounds based on app state (foreground vs. background), limiting flexibility in notification management.

Expected behavior

  • For background notifications, I want them to be accompanied by a sound.
  • For foreground notifications, I prefer to handle them manually (e.g., via local notifications or custom logic) without an automatic sound.

Reproduction

https://github.com/sinanverve7/fcm-issue-reproduction

Steps to reproduce

  1. i have wrote the configuration iam using in project in capacitor.config.js.
  2. just run this project on iphone.
  3. enable fcm on this app
  4. keill the app
  5. send the fcm message to the app.
  6. check the notification sound on background mode

Other information

i have integrated capacitor-firebase/authentication aswell ( 5.1.0 )

Capacitor doctor

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 5.7.4
@capacitor/core: 5.7.4
@capacitor/android: 5.7.4
@capacitor/ios: 5.7.4

Installed Dependencies:

@capacitor/android: not installed
@capacitor/cli: 4.8.1
@capacitor/core: 4.8.1
@capacitor/ios: 4.8.1

[success] iOS looking great! 👌

Before submitting

  • I have read and followed the bug report guidelines.
  • I have attached links to possibly related issues and discussions.
  • I understand that incomplete issues (e.g. without reproduction) are closed.

I am closing this issue as this is the intended behavior and not a bug.

Hi @robingenz
so if i didnot give "sound" as a parameter in presentationOptions like below, will it mute both foreground and background notification?
i were trying to migrate from capacitor-pushnotification to handle silent notification in ios.
previously only our background notification used to come up with sound.
FirebaseMessaging: { presentationOptions: [], },

hi @robingenz in my case it silences the background notification aswell.? it is opposite to the intended behaviour.

I misread your issue.

so if i didnot give "sound" as a parameter in presentationOptions like below, will it mute both foreground and background notification?

No, this only affects the foreground. I have just tested it and had no problems. Make sure you set the sound property when sending the notification. Here is my nodejs code that I tested it with:

import { initializeApp, cert } from "firebase-admin/app";
import { Message, getMessaging } from "firebase-admin/messaging";

initializeApp({
  credential: cert("<PATH>"),
});

// This FCM token comes from the Capacitor Firebase Cloud Messaging plugin.
const token = "<TOKEN>";

const message: Message = {
  notification: {
    title: "Capacitor Firebase Messaging",
    body: "Hello world!",
  },
  apns: {
    payload: {
      aps: {
        sound: "default",
      },
    },
  },
  token: token,
};

// Send a message to the device corresponding to the provided FCM token
getMessaging()
  .send(message)
  .then((response) => {
    console.log("Successfully sent message: ", response);
  })
  .catch((error) => {
    console.log("Error sending message: ", error);
  });

For more information, take a look at this Push Notification Guide for Capacitor.

i have checked above backend code with our backend dev.we tried with both legacy firebase api and new api in both cases sound is muted.in the presentation options only "sound" parameter seems to have issue.the same notification gives sound with @capacitor/push-notifications.is there any other cases that blocks sound associated with push-notification ?

{
    "notification": {
      "body": "",
      "id": "value",
      "title": "check your card",
      "subtitle": "",
      "data": {
        "google.c.sender.id": "value",
        "aps": {
          "sound": "default",
          "alert": {
            "title": "check your card",
            "body": ""
          }
        },
        "gcm.notification.params": "value",
        "google.c.fid": "value",
        "google.c.a.e": "value",
        "gcm.message_id": "value"
      }
    }
  }

Have you tested it with the official demo app?

No i havenot tested yet

@robingenz I'm not sure if this is the right place to ask. But is it also intended that the requestPermission() will automatically return 'denied' when you have no presentationOptions configured? I would still like to show the permission prompt even when I don't show the notifications in the foreground.

@roosalyn Yes, this is intended. If no presentation options are defined, you do not need any permissions. If you have more questions, please create a discussion.