react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
745 stars 285 forks source link

NSInvalidArgumentException - Invalid type in JSON write (UNNotificationSound) #338

Closed randomtoni closed 3 years ago

randomtoni commented 3 years ago

Hello,

I'm getting the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (UNNotificationSound)' *** First throw call stack: (0x1b58a4654 0x1b55c6bcc 0x1b5d1cb7c 0x1b5d1f4a0 0x1b58f1c14 0x1b577c51c 0x1b5d1e9d0 0x1b5bd400c 0x1b5bd3c90 0x104d1eda4 0x104d1ed28 0x1b5800504 0x1b580054c 0x1b57ff8a4 0x1b57ff57c 0x1b577c2d4 0x1b57feedc 0x1b5b6c974 0x1049a4864 0x1045c0c0c 0x1049c94e8 0x10490cc08 0x1b996c200 0x1b8f6dab0 0x1b8f6eb64 0x1b94f4a08 0x1baa2c0a8 0x1baa50684 0x1baa35bfc 0x1baa505b8 0x107457730 0x10745abb0 0x1baa74850 0x1baa7451c 0x1baa74a44 0x1b5822ad8 0x1b5822a30 0x1b58221b8 0x1b581d1e8 0x1b581cba8 0x1bf993344 0x1b99583e4 0x1045c0cd8 0x1b56a48f0) libc++abi.dylib: terminating with uncaught exception of type NSException

These are the parameters that I'm sending to the method.

  PushNotificationIOS.addNotificationRequest({
    id: "0",
    title: "title",
    subtitle: "",
    body: "body body",
    badge: 1,
    // fireDate: new Date(),
    // repeats: ,
    // sound: ,
    // category: ,
    // isSilent: ,
    userInfo: {},
  })

This issue can be overruled if I set isSilent: true, but I want these notification to play a sound if the user is playing sounds. What can be done to get the default sound playing?

Thank you

randomtoni commented 3 years ago

Hello,

I was able to solve the issue by editing the following lines in the RCTConvert+Notification.m file:

content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
+    NSString *soundName = [content.userInfo objectForKey:@"sound"];
+    BOOL notEmpty = (soundName == (id)[NSNull null] || soundName.length == 0);
+
     if (!isSilent) {
         if (isCritical) {
             if (criticalSoundVolume) {
-                content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]] withAudioVolume:criticalSoundVolume] : [UNNotificationSound defaultCriticalSoundWithAudioVolume:criticalSoundVolume];
+                content.sound = notEmpty ? [UNNotificationSound criticalSoundNamed:soundName withAudioVolume:criticalSoundVolume] : [UNNotificationSound defaultCriticalSoundWithAudioVolume:criticalSoundVolume];
             } else {
-                content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultCriticalSound];
+                content.sound = notEmpty ? [UNNotificationSound criticalSoundNamed:soundName] : [UNNotificationSound defaultCriticalSound];
             }
         } else {
-            content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound];
+            content.sound = notEmpty ? [UNNotificationSound soundNamed:soundName] : [UNNotificationSound defaultSound];
         }
     }