6

私はUNUserNotificationCenterios 10に使用しています。テストのために、現在の時刻から10秒間ローカル通知を設定しています。

これは私が試したものです、

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

-(void) set10Notifs
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
    #if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    [calendar setTimeZone:[NSTimeZone localTimeZone]];        

    NSDateComponents *components = [calendar components:NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];

    UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
    objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Prayer!" arguments:nil];
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Time now"
                                                                        arguments:nil];
    objNotificationContent.sound = [UNNotificationSound defaultSound];

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


    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
                                                                          content:objNotificationContent trigger:trigger];
    UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
    [userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Local Notification succeeded");
        }
        else {
            NSLog(@"Local Notification failed");
        }
    }];
#endif
    }
}

「ローカル通知が成功しました」というログが表示されます。しかし、ローカル通知はデバイスで発生していません。

Appdelegateで、追加しました

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    return YES;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSLog(@"Notification is triggered");
     completionHandler(UNNotificationPresentationOptionBadge);
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
    NSLog(@"User Info : %@",response.notification.request.content.userInfo);
    completionHandler();
}

私は何を間違えましたか?アプリの通知が発生しないのはなぜですか?

4

3 に答える 3

0

または、カレンダー トリガーの代わりにタイム トリガーを使用する

于 2016-10-29T03:57:08.653 に答える