0

EDIT: I am setting exactly 365 local notifications UNUserNotification (one for each day a year). Button is calling at one time:

[self name1];
[self name2];
...
[self name365];
  • one name for every day. If I try only eg. 3 days, it works perfectly. If I call all days (365x), it fires only the last local notification (365. only) - day 1-364 is missed (not fired). Any ideas?

Code:

-(void)Name1{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.hour = 9;
    comps.minute = 0;
    comps.day = 23;
    comps.month = 10;
 
    UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
    objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"NAME" arguments:nil];
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"text"
                                                                        arguments:nil];
    objNotificationContent.sound = [UNNotificationSound soundNamed:@"notif_bobicek.mp3"];
    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents: comps repeats:YES];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requestNotificationForName1"
                                                                          content:objNotificationContent trigger:trigger];
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
      if (!error) { NSLog(@"Local Notification succeeded"); }
      else { NSLog(@"Local Notification failed"); }}];
}

.

-(void)Name2{ ... // same code
-(void)Name365{... // same code

Note: Identifier is different for each scheduled local notification.

Jiri Svec
  • 147
  • 1
  • 1
  • 12
  • I think you should try to add a counter rather than have 365 duplicate pieces of code. What if you need to change it? If you must have 365 codes then try to create it using #define in stead. At least you can change it in just one way. I’m not familiar with local notifications but maybe they need different request identifiers? – skaak Oct 25 '20 at 05:45

1 Answers1

0

This does not answer your question directly and you really should parameterise your approach, but what I will show you can help a lot. I want to illustrate clearly what I meant with using #define in my comment.

Using #define is a powerful way to solve some tricky problems and even here it can make your life a lot easier. Think of #define as a type of search and replace, which it literally used to be, and here you can use that to define your message ONCE and then replace it 365 times in stead of creating 365 different messages. Here is an outline.

// MyClass.m
#import "MyClass.h"

// The \ indicates the macro continues on the next line
// Note this becomes just one long string without linebreaks so you can
// not use // to comment, you must use /* ... */ to comment
// The ## indicates it is a string concatenation
#define MYFUNC( DAY )                                           \
- ( void ) name ## DAY {                                        \
    /* Now you are inside nameDAY e.g. name1, name2 ... etc */  \
    NSDateComponents *comps = [[NSDateComponents alloc] init];  \
    comps.hour = 9;                                             \
    comps.minute = 0;                                           \
    comps.day = 23;                                             \
    comps.month = 10;                                           \
    /* Some examples below */                                   \
    NSUInteger day = DAY;                                       \
    NSString * s = [NSString stringWithFormat:@"ID:%lu", DAY];  \
    NSLog( @"Inside %@", s );                                   \
};

@implementation MyClass

// Now you can create them as below
MYFUNC( 1 )
MYFUNC( 2 )
MYFUNC( 3 )
MYFUNC( 4 )
// etc

// They are now defined and can be called as normal
- ( void ) test
{
    [self name1];
    [self name2];
    [self name3];
    // etc
}

@end

If you parameterise your approach but still need 365 functions you can e.g. expand the #define by adding more parameters. Anyhow, for more info see this really nice reference.

https://en.wikibooks.org/wiki/C_Programming/Preprocessor_directives_and_macros

HIH

skaak
  • 2,988
  • 1
  • 8
  • 16