【问题标题】:Setting multiple UNUserNotification firing only the last notification设置多个 UNUserNotification 仅触发最后一个通知
【发布时间】:2020-10-23 22:02:54
【问题描述】:

编辑:我正在设置 365 个本地通知 UNUserNotification(一年每天一个)。按钮一次调用:

[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

注意:每个计划的本地通知的标识符都不同。

【问题讨论】:

  • 我认为你应该尝试添加一个计数器,而不是有 365 段重复的代码。如果需要更改怎么办?如果您必须有 365 代码,请尝试使用 #define 创建它。至少你可以用一种方式改变它。我不熟悉本地通知,但也许它们需要不同的请求标识符?

标签: objective-c unusernotificationcenter unusernotification


【解决方案1】:

这并不能直接回答您的问题,您确实应该对您的方法进行参数化,但我将向您展示的内容对您有很大帮助。我想清楚地说明我在评论中使用#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多