Swift 4 Repeating Local Notifications





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying it make a notification that will trigger every day at the same time (7:00AM). I am able to get the notification to trigger if I use UNTimeIntervalNotificationTrigger but not UNCalendarNotificationTrigger which is what I really want. This is what I have currently:



In AppDelegate.swift I have:



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}

return true
}


And I remembered to import UserNotifications too



When the user presses a button in ViewController.swift this is run:



let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil


Could you please explain to me what is wrong here, and why UNTimeIntervalNotificationTrigger works but UNCalendarNotificationTrigger doesn't?










share|improve this question

























  • Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

    – Joakim Danielson
    Jan 4 at 9:20













  • @JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

    – Coedice
    Jan 4 at 9:35













  • How do you set up the delegate?

    – Joakim Danielson
    Jan 4 at 9:41











  • Simply print the triggerTime, and you’ll see what type of problem.

    – Mannopson
    Jan 4 at 11:45


















1















I am trying it make a notification that will trigger every day at the same time (7:00AM). I am able to get the notification to trigger if I use UNTimeIntervalNotificationTrigger but not UNCalendarNotificationTrigger which is what I really want. This is what I have currently:



In AppDelegate.swift I have:



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}

return true
}


And I remembered to import UserNotifications too



When the user presses a button in ViewController.swift this is run:



let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil


Could you please explain to me what is wrong here, and why UNTimeIntervalNotificationTrigger works but UNCalendarNotificationTrigger doesn't?










share|improve this question

























  • Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

    – Joakim Danielson
    Jan 4 at 9:20













  • @JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

    – Coedice
    Jan 4 at 9:35













  • How do you set up the delegate?

    – Joakim Danielson
    Jan 4 at 9:41











  • Simply print the triggerTime, and you’ll see what type of problem.

    – Mannopson
    Jan 4 at 11:45














1












1








1








I am trying it make a notification that will trigger every day at the same time (7:00AM). I am able to get the notification to trigger if I use UNTimeIntervalNotificationTrigger but not UNCalendarNotificationTrigger which is what I really want. This is what I have currently:



In AppDelegate.swift I have:



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}

return true
}


And I remembered to import UserNotifications too



When the user presses a button in ViewController.swift this is run:



let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil


Could you please explain to me what is wrong here, and why UNTimeIntervalNotificationTrigger works but UNCalendarNotificationTrigger doesn't?










share|improve this question
















I am trying it make a notification that will trigger every day at the same time (7:00AM). I am able to get the notification to trigger if I use UNTimeIntervalNotificationTrigger but not UNCalendarNotificationTrigger which is what I really want. This is what I have currently:



In AppDelegate.swift I have:



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}

return true
}


And I remembered to import UserNotifications too



When the user presses a button in ViewController.swift this is run:



let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil


Could you please explain to me what is wrong here, and why UNTimeIntervalNotificationTrigger works but UNCalendarNotificationTrigger doesn't?







ios swift notifications






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 9:14







Coedice

















asked Jan 4 at 9:06









CoediceCoedice

64




64













  • Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

    – Joakim Danielson
    Jan 4 at 9:20













  • @JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

    – Coedice
    Jan 4 at 9:35













  • How do you set up the delegate?

    – Joakim Danielson
    Jan 4 at 9:41











  • Simply print the triggerTime, and you’ll see what type of problem.

    – Mannopson
    Jan 4 at 11:45



















  • Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

    – Joakim Danielson
    Jan 4 at 9:20













  • @JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

    – Coedice
    Jan 4 at 9:35













  • How do you set up the delegate?

    – Joakim Danielson
    Jan 4 at 9:41











  • Simply print the triggerTime, and you’ll see what type of problem.

    – Mannopson
    Jan 4 at 11:45

















Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

– Joakim Danielson
Jan 4 at 9:20







Have you tried adding a check for any error in the completion handler the same way you have for your first code example? And how do you know it. doesn't work?

– Joakim Danielson
Jan 4 at 9:20















@JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

– Coedice
Jan 4 at 9:35







@JoakimDanielson I did what you suggested and it reports no error. I believe it doesn't work because I changed my computer's time to the time that it should trigger of tomorrow, and it didn't notify me. I also tried the beginning of every hour just to rule out any possible time-zone related issues

– Coedice
Jan 4 at 9:35















How do you set up the delegate?

– Joakim Danielson
Jan 4 at 9:41





How do you set up the delegate?

– Joakim Danielson
Jan 4 at 9:41













Simply print the triggerTime, and you’ll see what type of problem.

– Mannopson
Jan 4 at 11:45





Simply print the triggerTime, and you’ll see what type of problem.

– Mannopson
Jan 4 at 11:45












2 Answers
2






active

oldest

votes


















0














I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. I would propose you to do it by initializing an empty DateComponent and only set the hours as you want.



So, could you try to do this :



let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

var triggerTime = DateComponents()
triggerTime.hour = 7

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


I did it from my own side and it works.



I hope it may help.






share|improve this answer
























  • Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

    – Joakim Danielson
    Jan 4 at 10:53











  • Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

    – Coedice
    Jan 4 at 11:13



















0














My original code works fine, I just needed to do my testing differently. For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035853%2fswift-4-repeating-local-notifications%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. I would propose you to do it by initializing an empty DateComponent and only set the hours as you want.



    So, could you try to do this :



    let content = UNMutableNotificationContent()
    content.title = "Do your daily review"
    content.badge = 1

    var triggerTime = DateComponents()
    triggerTime.hour = 7

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

    let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


    I did it from my own side and it works.



    I hope it may help.






    share|improve this answer
























    • Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

      – Joakim Danielson
      Jan 4 at 10:53











    • Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

      – Coedice
      Jan 4 at 11:13
















    0














    I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. I would propose you to do it by initializing an empty DateComponent and only set the hours as you want.



    So, could you try to do this :



    let content = UNMutableNotificationContent()
    content.title = "Do your daily review"
    content.badge = 1

    var triggerTime = DateComponents()
    triggerTime.hour = 7

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

    let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


    I did it from my own side and it works.



    I hope it may help.






    share|improve this answer
























    • Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

      – Joakim Danielson
      Jan 4 at 10:53











    • Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

      – Coedice
      Jan 4 at 11:13














    0












    0








    0







    I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. I would propose you to do it by initializing an empty DateComponent and only set the hours as you want.



    So, could you try to do this :



    let content = UNMutableNotificationContent()
    content.title = "Do your daily review"
    content.badge = 1

    var triggerTime = DateComponents()
    triggerTime.hour = 7

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

    let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


    I did it from my own side and it works.



    I hope it may help.






    share|improve this answer













    I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. I would propose you to do it by initializing an empty DateComponent and only set the hours as you want.



    So, could you try to do this :



    let content = UNMutableNotificationContent()
    content.title = "Do your daily review"
    content.badge = 1

    var triggerTime = DateComponents()
    triggerTime.hour = 7

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

    let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


    I did it from my own side and it works.



    I hope it may help.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 at 10:01









    Antoine RucquoyAntoine Rucquoy

    8319




    8319













    • Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

      – Joakim Danielson
      Jan 4 at 10:53











    • Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

      – Coedice
      Jan 4 at 11:13



















    • Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

      – Joakim Danielson
      Jan 4 at 10:53











    • Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

      – Coedice
      Jan 4 at 11:13

















    Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

    – Joakim Danielson
    Jan 4 at 10:53





    Why would the init method be a problem? Please elaborate on that preferable with some proof for your claims.

    – Joakim Danielson
    Jan 4 at 10:53













    Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

    – Coedice
    Jan 4 at 11:13





    Thanks for your response, it hasn't fixed my problem. Could you provide a link to your project so I can try to spot what else you've done differently?

    – Coedice
    Jan 4 at 11:13













    0














    My original code works fine, I just needed to do my testing differently. For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones.






    share|improve this answer




























      0














      My original code works fine, I just needed to do my testing differently. For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones.






      share|improve this answer


























        0












        0








        0







        My original code works fine, I just needed to do my testing differently. For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones.






        share|improve this answer













        My original code works fine, I just needed to do my testing differently. For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 5 at 0:52









        CoediceCoedice

        64




        64






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035853%2fswift-4-repeating-local-notifications%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'