Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Sound is not working when app is in background or terminated #907

Closed
MilanRibadiya opened this issue Dec 21, 2023 · 1 comment
Closed
Labels
bug Something isn't working closed by bot This issue was automatically closed due to inactivity in triage This issue is waiting for first service response

Comments

@MilanRibadiya
Copy link

Rules

To report a bug and help us to provide the solution as fast as possible, please follow the instructions below:

Custom Sound is not working when app is in background or terminated

A clear and concise description of what the bug is.

OBS: avoid to use the expression "its not working" or "is working" or any other variation. These expressions can mean anything and at same time doesn´t bring any useful information. As soon you rewrite these expressions, you naturally going to be more precise and clear.

2 - Provide us as much information as possible

To help us to understand your issue, provide with your bug report all the relevant informations, such as:

  • Your plugins list inside the file pubspec.yaml (at least all notification plugins)
  • firebase_messaging: ^14.7.5
    awesome_notifications: ^0.8.2
  • What platform did you found the issue
  • Android
  • What devices did you found the issue
  • All
  • How are you initializing the plugin
    AwesomeNotifications().initialize(
    null,
    [
    NotificationChannel(
    channelKey: "alert",
    channelName: "alert",
    channelDescription: "This is for Person Detection",
    playSound: true,
    soundSource: "resource://raw/res_alert",
    criticalAlerts: true,
    importance: NotificationImportance.High,
    enableVibration: true,
    onlyAlertOnce: false,
    defaultRingtoneType: DefaultRingtoneType.Ringtone,
    ),
    ],
    );

  • How are you creating the notification

Future handleBackgroundMessage(RemoteMessage message) async {
String? title = message.notification?.title;
String? body = message.notification?.body;

print("Title : ${title}");
print("Body : ${body}");
print("Payload : ${message.data}");

await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 1359,
channelKey: "alert",
title: title,
body: body,
wakeUpScreen: true,
category: NotificationCategory.Call,
customSound: "resource://raw/res_alert",
autoDismissible: false,
criticalAlert: true,
),
actionButtons: [
NotificationActionButton(key: "ok", label: "OK"),
NotificationActionButton(key: "cancel", label: "Cancel"),
]
);
}

  • The output of flutter doctor command

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.2, on macOS 13.5.1 22G90 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.84.2)
[✓] Connected device (3 available)
! Error: Browsing on the local area network for Brit’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
! Error: Browsing on the local area network for Rahil’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!

OBS: To post a source code in your issue, please use the markdown below:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  AwesomeNotifications().initialize(
    null,
    [
      NotificationChannel(
          channelKey: "alert",
          channelName: "alert",
          channelDescription: "This is for Person Detection",
          playSound: true,
          soundSource: "resource://raw/res_alert",
        criticalAlerts: true,
        importance: NotificationImportance.High,
        enableVibration: true,
        onlyAlertOnce: false,
        defaultRingtoneType: DefaultRingtoneType.Ringtone,
      ),
    ],
  );
  await PushNotificationService().initNotification();
  FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
  runApp(const MyApp());
}

'''

class PushNotificationService {
final firebaseInstance = FirebaseMessaging.instance;

Future initNotification() async {
await firebaseInstance.requestPermission(
alert: true, badge: true, sound: true, criticalAlert: true);
await firebaseInstance.subscribeToTopic("alert");
final fcmToken = await firebaseInstance.getToken();
print(fcmToken);

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
  String? title = message.notification?.title;
  String? body = message.notification?.body;

  print("Title : ${title}");
  print("Body : ${body}");
  print("Payload : ${message.data}");

  await AwesomeNotifications().createNotification(
    content: NotificationContent(
      id: 1359,
      channelKey: "alert",
      title: title,
      body: body,
      wakeUpScreen: true,
      category: NotificationCategory.Call,
      customSound: "resource://raw/res_alert",
      autoDismissible: false,
      criticalAlert: true,
    ),
    actionButtons: [
      NotificationActionButton(key: "ok", label: "OK"),
      NotificationActionButton(key: "cancel", label: "Cancel"),
    ]
  );
});

}
}

Future handleBackgroundMessage(RemoteMessage message) async {
String? title = message.notification?.title;
String? body = message.notification?.body;

print("Title : ${title}");
print("Body : ${body}");
print("Payload : ${message.data}");

await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 1359,
channelKey: "alert",
title: title,
body: body,
wakeUpScreen: true,
category: NotificationCategory.Call,
customSound: "resource://raw/res_alert",
autoDismissible: false,
criticalAlert: true,
),
actionButtons: [
NotificationActionButton(key: "ok", label: "OK"),
NotificationActionButton(key: "cancel", label: "Cancel"),
]
);
}

'''

3 - How To Reproduce the Error

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

...or if you prefer, you can provide us a simple example app on GitHub with the issue already reproduced instead of the steps 2 and 3.

4 - Expected behavior

I want to set custom sound in background notification in handle background message.

@MilanRibadiya MilanRibadiya added bug Something isn't working in triage This issue is waiting for first service response labels Dec 21, 2023
Copy link

This issue was automatically closed due inactivity, but can be reopened at any time.

@github-actions github-actions bot added the closed by bot This issue was automatically closed due to inactivity label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working closed by bot This issue was automatically closed due to inactivity in triage This issue is waiting for first service response
Projects
None yet
Development

No branches or pull requests

1 participant