capawesome-team / capacitor-firebase

⚡️ Firebase plugins for Capacitor. Supports Android, iOS and the Web.
https://capawesome.io/plugins/firebase/
Apache License 2.0
366 stars 90 forks source link

bug: Issue with presentationOptions Config Affecting Background Notification Sound #601

Closed sinanverve7 closed 3 months ago

sinanverve7 commented 3 months ago

Plugin(s)

Version

5.4.1

Platform(s)

Current behavior

When configuring FirebaseMessaging's presentationOptions in my Capacitor app:

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.

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

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

robingenz commented 3 months ago

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

sinanverve7 commented 3 months ago

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: [], },

sinanverve7 commented 3 months ago

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

robingenz commented 3 months ago

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.

sinanverve7 commented 3 months ago

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"
      }
    }
  }
robingenz commented 3 months ago

Have you tested it with the official demo app?

sinanverve7 commented 3 months ago

No i havenot tested yet

roosalyn commented 2 months ago

@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.

robingenz commented 2 months ago

@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.