【问题标题】:iOS local notification not firing second time but shows in getpendingnotificationrequestsiOS 本地通知未第二次触发,但显示在 getpendingnotificationrequests 中
【发布时间】:2017-10-23 07:01:31
【问题描述】:

我已经实现了 iOS 10 本地通知,它适用于第一个警报,但不适用于其他警报。我已经导入 iOS 10 库,实现了委托,第一次在委托中接收它,但它只是稍后没有触发。

我的日期格式如下:2017-05-28 14:04:07 +0000

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];
    //(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];

   NSDate *todaySehri = [calendar dateFromComponents:components];


 UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];

            objNotificationContent.title = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"Local.."]] arguments:nil];


                  objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Incoming Local Notification" arguments:nil];


            objNotificationContent.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@",soundFileBtn.titleLabel.text]]; // [UNNotificationSound defaultSound];//soundFileBtn.titleLabel.text;// [UNNotificationSound defaultSound];
            NSDictionary *dict = @{@"alarmID":self.myKey,
                                                   @"SOUND_TYPE":[NSString stringWithFormat:@"%li",(long)self.audio_segment.selectedSegmentIndex]
                                                   };
            NSArray *array = [NSArray arrayWithObjects:dict, nil];
            NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];


            [objNotificationContent setUserInfo:data];

            //update application icon badge number
            objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 0);

            NSCalendar *cal = [NSCalendar currentCalendar];

              NSDateComponents *compss= [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
                          fromDate:todaySehri];

             UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:compss repeats:NO];

            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[todaySehri description]
                                                                                  content:objNotificationContent trigger:trigger];
            //schedule localNotification
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                if (!error) {
                    NSLog(@"Local Notification succeeded");
                }
                else {
                    NSLog(@"Local Notification failed");
                }
            }];

【问题讨论】:

  • 尝试打印您为每个触发器设置的identifier,以检查它们是否不同。另外,您如何测试通知?通过手动更改设备上的时间或等待时间?
  • 标识符在我的 NSLog 中不同。即使我在我的标识符中附加了一个额外的随机字符串。我正在检查模拟器上的警报。我设置了3个单独的闹钟。我将我的 macbook 时间设置为接近第一个闹钟。当它工作时,我再次将我的 macbook 时间更改为接近第二个闹钟并运行我的项目。但它不适用于第二个或其他警报。 @bhakti123
  • 检查是否所有的警报都安排好了。他们的下一个触发日期是什么时候。然后尝试根据该触发日期更改时间,看看是否有效。
  • 预定是什么意思?我怎么查?我的 compss 包含我设置触发的警报的触发点。 @bhakti123
  • 您可以使用getpendingnotificationrequests查看定时闹钟

标签: objective-c xcode ios10 local unusernotificationcenter


【解决方案1】:

如何将您的触发器YES 设置为重复?另外,检查一下。回复repeating local notifications

【讨论】:

  • 不!重复 (YES) 表示您希望每天或之后的任何特定时间查看此内容。但在这里我将通知单独设置为单独的一天。假设,我在 3 个不同的时间创建 3 个通知,为期 3 天。
  • 在这种情况下,您应该保留三个标识符来分隔不同的通知
  • 是的,我的标识符是 requestWithIdentifier-> [todaySehri description],它在每个触发器中都是不同的字符串。那我该怎么办? :(
猜你喜欢
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多