9

UILocalNotification ではNSCalendarUnitMinute、繰り返しのように使用します ..... しかし、iOS 10 UserNotification docNSCalendarUnitMinuteで見つけることができません ... iOS 10 で繰り返しのように使用するにはどうすればよいUserNotificationですか?

これは、午後 8 時 30 分にローカル通知をスケジュールし、1 分ごとに繰り返すコードです。

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.textField.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
4

3 に答える 3

1

の代わりにaUNCalendarNotificationTriggerを使用します。DateComponentsNSCalendar

var date = DateComponents()
date.hour = 8
date.minute = 30

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()
// edit your content

let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)

インスタンスで日付を設定し、イニシャライザのパラメータでDateComponents通知を繰り返すかどうかを指定します。repeatsUNCalendarNotificationTrigger

UNCalendarNotificationTrigger ドキュメント

Apple Docs にタイプミスがあることに注意してください (6/13 時点)。NSDateComponentsの代わりに使用する場合は、パラメーターでDateComponents明示的にキャストする必要があります。date as DateComponentsdateMatching

あなたのコメントに応えて、私はあなたが望む動作 (繰り返し通知の頻度を変更する) が によってサポートされているとは思わないUNCalendarNotificationTrigger.

于 2016-06-14T06:21:46.223 に答える
1

これが私の最初の投稿です。

iOS 10 のアプリには 1 分間の繰り返し間隔が必要であり、回避策として古いフレームワークと新しいフレームワークを組み合わせて使用​​しました。

  1. 古いもので繰り返しローカル通知をスケジュールする:

    UILocalNotification *ln = [[UILocalNotification alloc] init];
    ...
    ln.repeatInterval = kCFCalendarUnitMinute;
    ln.userInfo = @{@"alarmID": ...}
    ...
    [[UIApplication sharedApplication] scheduleLocalNotification:ln];

これにより、UNLegacyNotificationTrigger を持つ UNNotification として表示される通常の繰り返し LN が作成およびスケジュールされます。

古いフレームワークと新しいフレームワークを接続するには、alarmID を使用しました。

試す:

UNUserNotificationCenter* center = [UNUserNotificationCenter 

currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
    NSLog(@"----------------- PendingNotificationRequests %i -------------- ", (int)requests.count);
    for (UNNotificationRequest *req in requests) {
        UNNotificationTrigger *trigger = req.trigger;
        NSLog(@"trigger %@", trigger);
    }
}];
  1. アクションへの応答と通知の起動、通知センターの操作:

新しいフレームワーク UNUserNotificationCenter のメソッド: willPresentNotification: didReceiveNotificationResponse:

  1. 通常の方法で両方のフレームワークに対して行ったリクエストの承認とカテゴリの登録。

お役に立てれば

于 2016-09-27T13:04:14.383 に答える