katzer / cordova-plugin-local-notifications

Cordova Local-Notification Plugin
Apache License 2.0
2.57k stars 1.75k forks source link

Adding new Local Notification crashes app (iOS8 Simulator) #413

Closed carmelodiventi closed 9 years ago

carmelodiventi commented 9 years ago

I've been trying to get Local Notifications to work within an Ionic app, but when arrive the notification app exit with this error:

Application Specific Information: *\ Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CDVDevice didReceiveLocalNotification:]: unrecognized selector sent to instance 0x7ca69520' terminating with uncaught exception of type NSException abort() called CoreSimulator 110.4 - Device: iPhone 6 - Runtime: iOS 8.1 (12B411) - DeviceType: iPhone 6

I state that I have tried various solutions proposed on this page "https://github.com/katzer/cordova-plugin-local-notifications/issues/309" , and checking the files I have the latest version that includes the so-called changes , can anyone help me?

thanks in advance

This my function:

  function SetNotification(IdMess,messaggio,dataScadenza){
              var idProdottoNotificato = IdMess.toString();
              window.plugin.notification.local.hasPermission(function () {

        dataScadenza.setHours(12);

        var now  = new Date().getTime(),
            _60_seconds_from_now = new Date(now + 60*1000);

        window.plugin.notification.local.add({ 
                id: idProdottoNotificato,
                date: _60_seconds_from_now,
                message: messaggio,
                sound : "www/sounds/beep.caf",
                badge: 1,
                repeat: "daily",
                autoCancel: true
            });

 });

  }

This my $scope:

      $scope.AggiungiProdotto= function(DispensaSelezionata) {
            var t = $scope.DispensaSelezionata;
            var n = $scope.NomeProdotto;
            var d = GetFormatDate($scope.DataScadenza);
            var g = $scope.GiorniPreavviso;
            var IdMess = "";
            var now = new Date(); 
            var DPreavv = FunSubtractDays(d,g);

            console.log(n, t, d, g);

            if(n==undefined || t == undefined){
                 $ionicPopup.alert({
                      title: 'Errore',
                      content: 'inserisci nome prodotto e/o dispensa'
                    });
                return false;
            }
            if(d==undefined){
             $ionicPopup.alert({
                      title: 'Errore',
                      content: 'inserisci la data di scadenza'
                    });
                return false;
            }
            else if (DPreavv< now) {
                console.log(DPreavv, d, now);
                    $ionicPopup.alert({
                          title: 'Errore',
                          content: 'La data di preavviso non può superare la data di scadenza'
                        });
                    return false;
                }
            else{

            dbRemindUse.transaction(function(e) {
                var i = "INSERT INTO prodotti (nome, iddispense, datascadenza, giornipreavviso) VALUES (?, ?, ?, ?)";
                e.executeSql(i, [n, t, d, g], function() {
                     $ionicPopup.alert({
                      title: 'Fatto',
                      content: 'Prodotto Inserito'
                     }).then(function(res) {
                                 window.location.reload(true);    
                    }); 

                        i = "SELECT last_insert_rowid() as IdProdottoIns";
                        e.executeSql(i, undefined, function(e, t) {
                            if (t.rows.length) {
                                for (var r = 0; r < t.rows.length; r++) {
                                            var row = t.rows.item(r);
                                            IdMess = row.IdProdottoIns;
                                        }
                                    //setta notifica
                                     SetNotification(IdMess, n + " In Scadenza", DPreavv);
                                    } else {

                                        return false;
                                    }   

                        }, error)

                }, error)

            })
        }
    }
MaZZly commented 9 years ago

Similar problem here..

Stacktrace: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFBoolean isEqualToString:]: unrecognized selector sent to instance 0x1043cc420' * First throw call stack: ( 0 CoreFoundation 0x0000000104122f35 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000107229bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010412a04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010408227c __forwarding + 988 4 CoreFoundation 0x0000000104081e18 _CF_forwarding_prep_0 + 120 5 Sense 0x0000000103dc17fa -[APPLocalNotificationOptions stringIsNullOrEmpty:] + 154 6 Sense 0x0000000103dc1123 -[APPLocalNotificationOptions soundName] + 99 7 Sense 0x0000000103dc44ba -[UILocalNotification(APPLocalNotification) init] + 410 8 Sense 0x0000000103dc42ca -[UILocalNotification(APPLocalNotification) initWithOptions:] + 154 9 Sense 0x0000000103dbc604 33-[APPLocalNotification schedule:]_block_invoke + 388 10 libdispatch.dylib 0x000000010794aba6 _dispatch_call_block_and_release + 12 11 libdispatch.dylib 0x00000001079687f4 _dispatch_client_callout + 8 12 libdispatch.dylib 0x000000010795315d _dispatch_root_queue_drain + 1097 13 libdispatch.dylib 0x00000001079545d9 _dispatch_worker_thread3 + 111 14 libsystem_pthread.dylib 0x0000000107cec6cb _pthread_wqthread + 729 15 libsystem_pthread.dylib 0x0000000107cea4a1 start_wqthread + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException

Adding a notification though safari debug works like a charm!

theunionInnovation commented 9 years ago

I was having a similar problem which was traced back to the following in UIApplication+APPLocalNotification.m (around line 200):

/**
 * Get local notification with ID.
 * @param id
 *      Notification ID
 */
- (UILocalNotification*) localNotificationWithId:(NSString*)id
{
    NSArray* notifications = self.localNotifications;
    for (UILocalNotification* notification in notifications)
    {
    //  this is causing a crash because of the notification.options.id being 'seen' as a number compared with a string. isEqualToString throws an exception.
   // Apologies: my naive kludge fix is to wrap notification.options.id in [NSString stringWithFormat:@"%@", notification.options.id] to convert it into something compatible with isEqualToString; BUT not sure if this actually, properly does the comparison wanted
   //     if ([notification.options.id isEqualToString:id]) {
         if ([[NSString stringWithFormat:@"%@", notification.options.id] isEqualToString:id]) {
            return notification;
        }
    }
    return NULL;
}

I have commented the code above explaining my kludge fix. Hope this helps anyone out. Please critique and feedback with any advice as to how to properly fix. TIA.

eduayala commented 9 years ago

Apparently, local notifications only work if your app is in background. You should try delaying your notification for X seconds then press the home button to exit the app. Then you'll get the notification. That works for me.

MaZZly commented 9 years ago

Sounds like the option object is missing when you try to add the notification.. I realized that my problem was due to a mismatch in variable name (and that rerunning the app in xcode doesn't rebuild the app so most of my debugging was worthless On 19 Feb 2015 22:02, "clawish" notifications@github.com wrote:

Does not works for me. Compiling is not possible and yields the error

property 'options' not found on object of type 'UILocalNotification *'

with your solution. :/

— Reply to this email directly or view it on GitHub https://github.com/katzer/cordova-plugin-local-notifications/issues/413#issuecomment-75125018 .

MaZZly commented 9 years ago

For me the notifications still work even if the app is in foreground On 20 Feb 2015 03:56, "Eduardo Ayala" notifications@github.com wrote:

Apparently, local notifications only work if your app is in background. You should try delaying your notification for X seconds then press the home button to exit the app. Then you'll get the notification. That works for me.

— Reply to this email directly or view it on GitHub https://github.com/katzer/cordova-plugin-local-notifications/issues/413#issuecomment-75177169 .

katzer commented 9 years ago

@MaZZly, your exception comes from a wrong data type you assign to sound: That property requires a string path and not a boolean value.

Something like

sound: "file://sound.mp3"

and not

sound: true|false
katzer commented 9 years ago

@iwakari, yours the same with id. When assigning a value from wrong data type you might end up with an exception sooner or later.

shi11 commented 9 years ago

I was having a similar problem. What solved my issue was setting id to a Number or a strictly numeric string. When my id had letters in it, it crashed. Hope that helps someone.

The documentation does say it can be NaN, but it's either broken or perhaps the documentation should change the id type to Number.

Note: The notification ID must be a string which can be converted to a number (that is, isNaN() returns false for). If the ID has an invalid format, it will silently be changed to 0 and will override an earlier one with the same ID.

katzer commented 9 years ago

The new 0.8.x release is a complete rewrite. If the issue still exists please reopen it.

You may also take a look into the new wiki.

Thanks!