iOS: notifications().displayNotification failed with error if sound is null
Issue
If call displayNotification with notification object which have sound as null,
then notifications/display_notification_error occurs.
The sound property will be null (and will be NSNull in objc world) when such as sending a remote notification from Firebase Notifications Web Console with no sound setting.
For example,
firebase.notifications().onNotification(async (notification: Notification) => {
// notification.sound === null
await firebase.notifications().displayNotification(notification).catch((err) => {
console.log(err);
});
});
then:
2019-02-19 19:08:40.676540+0900 REDACTED_APP_NAME[1368:750145] { [Error: Failed to display notificaton]
framesToPop: 1,
code: 'notifications/display_notification_error',
domain: 'NSCocoaErrorDomain',
userInfo: { NSDebugDescription: 'connection to service named com.apple.usernotifications.usernotificationservice' },
nativeStackIOS:
[ '0 REDACTED_APP_NAME 0x0000000104a494e4 RCTJSErrorFromCodeMessageAndNSError + 152',
'1 REDACTED_APP_NAME 0x000000010499166c __41-[RCTModuleMethod processMethodSignature]_block_invoke_2.218 + 176',
'2 REDACTED_APP_NAME 0x00000001047d1bec __65-[RNFirebaseNotifications displayNotification:resolver:rejecter:]_block_invoke + 136',
'3 UserNotifications 0x00000001ad6296c0 <redacted> + 40',
'4 libdispatch.dylib 0x00000001a2926d74 <redacted> + 32',
'5 libdispatch.dylib 0x00000001a292830c <redacted> + 20',
'6 libdispatch.dylib 0x00000001a292f6fc <redacted> + 552',
'7 libdispatch.dylib 0x00000001a2930280 <redacted> + 428',
'8 libdispatch.dylib 0x00000001a2938524 <redacted> + 588',
'9 libsystem_pthread.dylib 0x00000001a2b27b38 _pthread_wqthread + 316',
'10 libsystem_pthread.dylib 0x00000001a2b2ddec start_wqthread + 4' ],
It seems that if sound is NSNull, go into if statement at this line
and UNNotificationSound instance is created by
[UNNotificationSound soundNamed:notification[@"sound"]];,
but the sound name must not be nil, so maybe the error will be occured.
Workaround
want to display with some sounds
set sound with setter:
notification.setSound("default");
want to display with no sounds
If you use some kind of Typed AltJS like TypeScript, setSound don't accept undefined, so you should newly create notification object and don't set sound:
firebase.notifications().onNotification(async (notification: Notification) => {
const newNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle || "")
.setBody(notification.body);
await firebase.notifications().displayNotification(newNotification).catch((err) => {
console.log(err);
});
});
Project Files
iOS
ios/Podfile:
- [ ] I'm not using Pods
- [x] I'm using Pods and my Podfile looks like:
# N/A
AppDelegate.m:
// N/A
Android
android/build.gradle:
// N/A
android/app/build.gradle:
// N/A
android/settings.gradle:
// N/A
MainApplication.java:
// N/A
AndroidManifest.xml:
<!-- N/A -->
Environment
- Platform that you're experiencing the issue on:
- [x] iOS
- [ ] Android
- [x] iOS but have not tested behavior on Android
- [ ] Android but have not tested behavior on iOS
- [ ] Both
- If known, the version of the platform are you experiencing the issue on:
iOS 12.0e.g. iOS 10 or Android API 28
- Operating System:
- [x] MacOS, version:
10.14.2 - [ ] Windows, version:
N/A - [ ] Other, please specify:
N/A
- [x] MacOS, version:
- Build Tools:
Xcode 10.1e.g. Xcode 10, Android Studio 3.2
React Nativeversion:0.58.4
React Native Firebaselibrary version:5.2.2
Firebasemodule(s) you're using that has the issue:- [x] N/A
- [ ] Authentication
- [ ] Analytics
- [ ] Cloud Firestore
- [ ] Cloud Messaging (FCM)
- [ ] Crashlytics
- [ ] Dynamic Links
- [ ] Functions Callable
- [ ] In App Messaging
- [ ] Indexing
- [ ] Invites
- [ ] Instance ID
- [ ] ML Kit
- [x] Notifications
- [ ] Performance Monitoring
- [ ] Realtime Database
- [ ] Remote Config
- [ ] Storage
- Are you using
TypeScript?- [ ] No
- [x] Yes, version:
3.3.3
- Are you using Expo, e.g.
ExpoKit?- [x] No
- [ ] Yes, I've not ejected
- [ ] Yes, but I have ejected to
ExpoKit - [ ] Yes, but I have ejected to vanilla React Native
- Expo version:
N/A
Think react-native-firebase is great? Please consider supporting the project with any of the below:
- 👉 Donate via Open Collective
- 👉 Follow
React Native FirebaseandInvertaseon Twitter - 👉 Star this repo on GitHub ⭐️
- 👉 Contribute; see our contributing guide
I'm experiencing same issue using version RNF 5.2.2 and RN 0.58.5.
Simple workaround for this issue is overriding notification _sound field with:
'default'string if you want to use default iOS sound,undefinedif you want to deliver silent notification,
Before passing it to the displayNotification function.
RNInfo:
React Native Environment Info:
Binaries:
Node: 11.8.0 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.7.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
IDEs:
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.3 => 16.8.3
react-native: 0.58.5 => 0.58.5
Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
@Salakar This is still an issue with RNF 5.2.3 and RN 0.59.2.
Can confirm this on 0.59.2 and rn-firebase 5.2.3 as well
Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.
This is still an issue with RNF 5.3.1 and RN 0.59.8.
@Salakar @Ehesp Please reopen this...
Confirmed for me as well.. thank you @kiyotakagoto, as @Simek mentioned simple workaround was to add
notification.setSound('default'); or notification.setSound(undefined);
before displaying the notification
firebase.notifications().displayNotification(notification)
This is indeed still an issue in 5.3.1.
Confirmed for me as well.. thank you @kiyotakagoto, as @Simek mentioned simple workaround was to add
notification.setSound('default');ornotification.setSound(undefined);before displaying the notification
firebase.notifications().displayNotification(notification)
This work like a charm