11

iOS 10 を使用してローカル通知をスケジュールしたいのですが、その方法を知りたいです。私はウェブ全体を見回しましたが、通知を登録して処理するためだけの手がかりを見つけ続けています。ローカル通知のスケジューリング用ではありません。

それで、誰もこれを行う方法を知っていますか?

4

2 に答える 2

21
  1. それを試してみてください。非推奨ですが、機能するコードです。iOS 10.0 より前に使用します。

    //Get all previous noti..
     NSLog(@"scheduled notifications: --%@----", [[UIApplication sharedApplication] scheduledLocalNotifications]);
    
     NSDate *now = [NSDate date];
     now = [now dateByAddingTimeInterval:60*60*24*7]; //7 for 7th day of the week.
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    [calendar setTimeZone:[NSTimeZone localTimeZone]];
     NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
    
    
     NSDate *SetAlarmAt = [calendar dateFromComponents:components];
    
    
     UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
     localNotification.fireDate = SetAlarmAt;
    
    
     NSLog(@"FIRE DATE --%@----",[SetAlarmAt description]);
    
     localNotification.alertBody =@"Alert";
    
     localNotification.alertAction = [NSString stringWithFormat:@"My test for Weekly alarm"];
    
     localNotification.userInfo = @{
                               @"alarmID":[NSString stringWithFormat:@"123"],
                               @"SOUND_TYPE":[NSString stringWithFormat:@"hello.mp3"]
                               };
    
      localNotification.repeatInterval=0; //[NSCalendar currentCalendar];
    
    
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
    1. iOS 10.0 以降の場合: UserNotifications フレームワークを試してください: フレームワークを追加し、 #import のようにインポートします。Appdelegate Didfinishluanch メソッドで。

      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
          [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                    completionHandler:^(BOOL granted, NSError * _Nullable error) {
                        if (!error) {
                            NSLog(@"request succeeded!");
                            [self testAlrt];
                        }
                    }];
      

ibaction またはメソッドで、それを記述してテストします。

 NSDate *now = [NSDate date];

// NSLog(@"NSDate--before:%@",now);

now = [now dateByAddingTimeInterval:60*60*24*7];

NSLog(@"NSDate:%@",now);

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

[calendar setTimeZone:[NSTimeZone localTimeZone]];

NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];

NSDate *todaySehri = [calendar dateFromComponents:components]; //unused



UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!"
                                                                    arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];

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


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


UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten"
                                                                      content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (!error) {
        NSLog(@"Local Notification succeeded");
    }
    else {
        NSLog(@"Local Notification failed");
    }
}];
于 2016-10-09T10:07:27.297 に答える