Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - iOS local notification not firing second time but shows in getpendingnotificationrequests

I have implemented iOS 10 local notification where it works for the first alarm but not for other. I have import iOS 10 library , implemented delegate , receiving it first time in the delegate but it just not firing later.

My date formate looks like : 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");
                }
            }];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about setting your trigger YES to repeat? Also, check this out. answer for repeating local notifications


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...