質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

830閲覧

ローカル通知について(dateMatchingとtimeInterval)

Daisuke0315

総合スコア23

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/05/17 05:04

困っていること

timeintervalだと正常に動く(通知が鳴る)がdateMatchingだと動かない。

#該当コード

swift

1 2 3import UIKit 4import UserNotifications 5 6class ViewController: UIViewController { 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 } 15 16 @IBAction func notifyTapped(_ sender: Any) { 17 let content = UNMutableNotificationContent() 18 content.title = "今から一分後に日時指定" 19 content.body = "日時指定" 20 content.sound = UNNotificationSound.default() 21 let 一分後 = DateComponents(minute:60) 22 var 通知を鳴らす時間date = Calendar.current.date(byAdding: 一分後, to: Date()) 23 var 通知を鳴らす時間datecompo = Calendar.current.dateComponents([.year, .month, .day,.hour,.minute], from: 通知を鳴らす時間date!) 24 let trigger = UNCalendarNotificationTrigger(dateMatching: 通知を鳴らす時間datecompo, repeats: false) 25 let request = UNNotificationRequest(identifier: "immediately", content: content, trigger: trigger) 26 print(request) 27 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 28 } 29 30 31 @IBAction func nofityLaterTapped(_ sender: Any) { 32 let content = UNMutableNotificationContent() 33 content.title = "3秒後" 34 content.body = "タイムインターバル" 35 content.sound = UNNotificationSound.default() 36 37 38 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) 39 let request = UNNotificationRequest(identifier: "later", content: content, trigger: trigger) 40 print(request) 41 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 42 } 43 44} 45 46

#動かないリクエストのコンソール出力
<UNNotificationRequest: 0x600002425980; identifier: immediately, content: <UNNotificationContent: 0x6000011b88f0; title: <redacted>, subtitle: (null), body: <redacted>, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x600000198380>, realert: 0, trigger: <UNCalendarNotificationTrigger: 0x600002a24920; dateComponents: <NSDateComponents: 0x6000028b2c50> {
Calendar Year: 2020
Month: 5
Leap Month: 0
Day: 17
Hour: 14
Minute: 58, repeats: NO>>

最後に

拙い質問で恐縮ですがヒントだけでも教えて頂ければ嬉しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問