微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设置更多本地通知问题

如何解决设置更多本地通知问题

我正在设置365个本地通知(每年每天一次)。按钮一次通话:

[self name1];
[self name2];
...
[self name365];
  • 每天一个名字。如果我只尝试例如。 3天,效果很好。如果我全天(365x)呼叫,它将仅触发最近的本地通知(仅365)-错过了1-364天(未触发)。有什么想法吗?

代码

-(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

解决方法

这不能直接回答您的问题,您确实应该参数化您的方法,但是我将向您展示的内容会有所帮助。我想清楚地说明我在评论中使用#define的意思。

使用#define是解决某些棘手问题的有力方法,即使在这里,它也可以使您的生活更加轻松。将#define视为一种搜索和替换类型,实际上它曾经是这种类型,在这里您可以使用它来定义消息一次,然后替换365次,而不是创建365种不同的消息。这是轮廓。

// 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

如果您对方法进行参数设置,但仍需要365个功能,则可以例如通过添加更多参数来扩展#define。无论如何,有关更多信息,请参阅此非常好的参考。

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

HIH

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。