2 local notifications registriert - nur 1 aktiv

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • 2 local notifications registriert - nur 1 aktiv

    hallo alle zusammen,

    ich habe bislang einen swift 2 code gehabt, welcher mir zwei local notifications registriert.
    nun habe ich meinen code etwas optimieren wollen - das hat auch soweit alles gut geklappt - allerdings wird jetzt nur ein local notification registiert.

    hier mein code:

    Quellcode

    1. localNotification.userInfo = ["UUID": SecondReminderString!, ]
    2. print(localNotification.userInfo)
    3. localNotification.fireDate = FirstLN
    4. print(localNotification.fireDate)
    5. localNotification.alertBody = "Hier kommt der Text"
    6. localNotification.soundName = UILocalNotificationDefaultSoundName
    7. localNotification.timeZone = NSTimeZone.defaultTimeZone()
    8. localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    9. UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    10. }
    11. localNotification.userInfo = ["UUID": FirstReminderString, ]
    12. print(localNotification.userInfo)
    13. localNotification.fireDate = SameDay
    14. print(localNotification.fireDate)
    15. localNotification.alertBody = "Hier kommt der Text"
    16. localNotification.soundName = UILocalNotificationDefaultSoundName
    17. localNotification.timeZone = NSTimeZone.defaultTimeZone()
    18. localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    19. UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    Alles anzeigen




    habe testweise "prints" hinzugefügt um die Werte zu checken.
    wenn der o.g. Code duchgelaufen ist, habe ich folgendes in der Konsole stehen:

    Quellcode

    1. Optional([UUID: X3TUWqXk54])
    2. Optional(2015-10-29 09:00:00 +0000)
    3. Optional([UUID: EOiP4toFLi])
    4. Optional(2015-10-31 09:00:00 +0000)


    wenn ich mir dann alle LocalNotifications anschauen möchte mit:

    Quellcode

    1. print(UIApplication.sharedApplication().scheduledLocalNotifications)


    allerdings zeigt die Ausgabe dann nur folgendes an:

    Quellcode

    1. Optional([<UIConcreteLocalNotification: 0x14cea9b90>{fire date = Samstag, 31. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, time zone = Europe/Berlin (MEZ) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Samstag, 31. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, user info = {
    2. UUID = EOiP4toFLi;
    3. }}])

    Sehe gerade den Wald vor lauter Bäumen nicht mehr. sieht jemand den Fehler?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Ghost108 ()

  • Kann es sein, dass Du die Werte von localNotification stumpf überschreibst, also dasselbe Objekt mit anderen Parametern an scheduleLocalNotification übergibst, und es deshalb nur einmalig hinzugefügt wird?
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Marco Feltmann schrieb:

    Kann es sein, dass Du die Werte von localNotification stumpf überschreibst, also dasselbe Objekt mit anderen Parametern an scheduleLocalNotification übergibst, und es deshalb nur einmalig hinzugefügt wird?
    Laut Doku sollte das unproblematisch sein:

    Apple Doku schrieb:

    The local notification object that you want to schedule. This object contains information about when to deliver the notification and what to do when that date occurs. The system keeps a copy of this object so you may release the object once it is scheduled.
    „Meine Komplikation hatte eine Komplikation.“
  • Ich hab es auch mal getestet, es ist tatsächlich nur 1 drin

    Quellcode

    1. UIApplication.sharedApplication().cancelAllLocalNotifications();
    2. let calendar = NSCalendar.currentCalendar()
    3. let startDate = NSDate()
    4. var notificationDate = calendar.dateByAddingUnit(NSCalendarUnit.Minute, value: 5, toDate: startDate, options: [])
    5. let localNotifaction = UILocalNotification()
    6. localNotifaction.alertTitle = "hi"
    7. localNotifaction.alertBody = "what a body!"
    8. localNotifaction.fireDate = notificationDate
    9. UIApplication.sharedApplication().scheduleLocalNotification(localNotifaction)
    10. notificationDate = calendar.dateByAddingUnit(NSCalendarUnit.Minute, value: 10, toDate: startDate, options: [])
    11. localNotifaction.alertTitle = "hi again"
    12. localNotifaction.alertBody = "cool body"
    13. localNotifaction.fireDate = notificationDate
    14. UIApplication.sharedApplication().scheduleLocalNotification(localNotifaction)
    15. print(UIApplication.sharedApplication().scheduledLocalNotifications?.count)
    Alles anzeigen

    Edit:

    So habe ich dann wie gewünscht meine 2 drin

    Quellcode

    1. UIApplication.sharedApplication().cancelAllLocalNotifications();
    2. let calendar = NSCalendar.currentCalendar()
    3. let startDate = NSDate()
    4. var notificationDate = calendar.dateByAddingUnit(NSCalendarUnit.Minute, value: 5, toDate: startDate, options: [])
    5. let localNotifaction = UILocalNotification()
    6. localNotifaction.alertTitle = "hi"
    7. localNotifaction.alertBody = "what a body!"
    8. localNotifaction.fireDate = notificationDate
    9. UIApplication.sharedApplication().scheduleLocalNotification(localNotifaction)
    10. notificationDate = calendar.dateByAddingUnit(NSCalendarUnit.Minute, value: 10, toDate: startDate, options: [])
    11. let anotherNotification = UILocalNotification()
    12. anotherNotification.alertTitle = "hi again"
    13. anotherNotification.alertBody = "cool body"
    14. anotherNotification.fireDate = notificationDate
    15. UIApplication.sharedApplication().scheduleLocalNotification(anotherNotification)
    16. print(UIApplication.sharedApplication().scheduledLocalNotifications?.count)
    Alles anzeigen
  • habe zwei prints gemacht,

    nach jeder local notification eine

    ausgabe nach der ersten LN:

    Quellcode

    1. Optional([<UIConcreteLocalNotification: 0x146131900>{fire date = Donnerstag, 29. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, time zone = Europe/Berlin (MEZ) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Donnerstag, 29. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, user info = {
    2. UUID = 1Kov6ywBMb;
    3. }}])


    nach der zweiten:



    Quellcode

    1. Optional([<UIConcreteLocalNotification: 0x156ad3220>{fire date = Donnerstag, 29. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, time zone = Europe/Berlin (MEZ) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Donnerstag, 29. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, user info = {
    2. UUID = lFpahElpsv;
    3. }}, <UIConcreteLocalNotification: 0x1569bd6c0>{fire date = Samstag, 31. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, time zone = Europe/Berlin (MEZ) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Samstag, 31. Oktober 2015 um 10:00:00 Mitteleuropäische Normalzeit, user info = {
    4. UUID = Bwue0yVzuU;
    5. }}])


    so wäre es eigentlich auch richtig.
    und der witz an der sache ist, jetzt auf einmal gehts ...

    ka wieso. aber jetzt ist alles korrekt

    NEHME ALLS zurück
    Jetzt ist wieder nur 1 LN drin -.-
    muss ich das verstehen??

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Ghost108 ()

  • dann muss ich wohl die localnotification 1x firstLN nennen und die andere secondLN
    komisch nur das es die ganze zeit auch anders ging.

    Mal ne andere Frage:
    kann ich eine LN anlegen, und mit Hilfe von Repeat sagen, dass diese 2 tage später noch ein mal angezeigt werden soll?
  • macmoonshine schrieb:

    Marco Feltmann schrieb:

    Kann es sein, dass Du die Werte von localNotification stumpf überschreibst, also dasselbe Objekt mit anderen Parametern an scheduleLocalNotification übergibst, und es deshalb nur einmalig hinzugefügt wird?
    Laut Doku sollte das unproblematisch sein:

    Apple Doku schrieb:

    The local notification object that you want to schedule. This object contains information about when to deliver the notification and what to do when that date occurs. The system keeps a copy of this object so you may release the object once it is scheduled.
    Für mich lässt das keine Rückschlüsse auf irgendwas zu.
    Diese LocalNotification ist ja meines Dafürhaltens kein Mutable Object und von dem her ist da eine Kopie doch recht irrelevant.
    Zumal die Dokumentation dies lediglich in Zusammenhang mit einem Release (!?) erwähnt. Das sagt über das Verhalten beim Hinzufügen von LocalNotifications mit identischer Adresse eigentlich nichts aus.

    Matz' Beitrag scheint meine Vermutung jedenfalls zu bestätigen. ;)
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Marco Feltmann schrieb:

    Diese LocalNotification ist ja meines Dafürhaltens kein Mutable Object und von dem her ist da eine Kopie doch recht irrelevant.
    Natürlich ist eine Notification ein veränderliches Objekt, auch wenn kein Mutable im Namen steht. Dieses Muster gibt es übrigens noch an anderen Stellen (z. B. Session-Konfiguration). Damit verhindert Apple, dass das Programm Eigenschaften der Benachrichtigung ändert, sobald sie in der Queue steckt.

    Marco Feltmann schrieb:

    Zumal die Dokumentation dies lediglich in Zusammenhang mit einem Release (!?) erwähnt.
    Das ist wohl ein Überbleibsel aus MRC-Zeiten: „so you may release the object“; also man darf das Objekt freigeben und muss es nicht halten, um einen Dangling Pointer zu verhindern.

    Marco Feltmann schrieb:

    Das sagt über das Verhalten beim Hinzufügen von LocalNotifications mit identischer Adresse eigentlich nichts aus.
    Was bedeutet denn „The system keeps a copy of this object“ deiner Meinung nach?

    Marco Feltmann schrieb:

    Matz' Beitrag scheint meine Vermutung jedenfalls zu bestätigen.
    Matz' Vermutung ist anscheinend die beste Erklärung für das Verhalten, wenn es denn stimmen würde:

    Quellcode

    1. UILocalNotification *theNotification = [UILocalNotification new];
    2. theNotification.timeZone = [NSTimeZone localTimeZone];
    3. theNotification.soundName = UILocalNotificationDefaultSoundName;
    4. theNotification.alertBody = @"Test";
    5. theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30.0];
    6. [inApplication scheduleLocalNotification:theNotification];
    7. NSLog(@"%@", inApplication.scheduledLocalNotifications);
    8. theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60.0];
    9. [inApplication scheduleLocalNotification:theNotification];
    10. NSLog(@"%@", inApplication.scheduledLocalNotifications);
    Alles anzeigen
    Ausgabe:

    Quellcode

    1. 2015-10-28 19:20:18.176 LN[85789:2342992] (
    2. "<UIConcreteLocalNotification: 0x7fece0805a10>{fire date = Wednesday, October 28, 2015 at 7:20:48 PM Central European Standard Time, time zone = Europe/Berlin (GMT+1) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, October 28, 2015 at 7:20:48 PM Central European Standard Time, user info = (null)}"
    3. )
    4. 2015-10-28 19:20:18.178 LN[85789:2342992] (
    5. "<UIConcreteLocalNotification: 0x7fece0902270>{fire date = Wednesday, October 28, 2015 at 7:20:48 PM Central European Standard Time, time zone = Europe/Berlin (GMT+1) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, October 28, 2015 at 7:20:48 PM Central European Standard Time, user info = (null)}",
    6. "<UIConcreteLocalNotification: 0x7fece09029b0>{fire date = Wednesday, October 28, 2015 at 7:21:18 PM Central European Standard Time, time zone = Europe/Berlin (GMT+1) offset 3600, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, October 28, 2015 at 7:21:18 PM Central European Standard Time, user info = (null)}"
    7. )
    Beachte die Speicheradressen der Benachrichtigungen im Log: Sie sind unterschiedlich; also Kopien. Die zweite Ausgabe enthält zwei Notifications; wie es sein soll. Anscheinend kopiert die Queue auch die Werte auch häufiger, wahrscheinlich bei jedem Auslesen.
    „Meine Komplikation hatte eine Komplikation.“
  • Okay.
    Da das 'k' in Swift für 'Konsistenz' steht erklärt das dann auch abschließend, warum sowohl Matz als auch Ghost108 mal zwei und mal eine Notification in ihrem Swift Code bekommen.

    (Schließlich wurde jetzt von Zweien im Swift Code festgestellt, dass es nur einmal drin landet – eventuell, weil das Ding via let definiert ist und mit neuerem Swift da das .copy() anders implementiert?)
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P