###前提・実現したいこと
リマインダーアプリを作りたく、User Notifications Frameworkを調べてコードを書いていたのですが、以下のエラーが出てしまいます。
###発生している問題・エラーメッセージ
content.title = "Title" // の所にExpected declarationとエラー let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) の content: content, // の所に Cannot use instance member 'content' within property initializer:prop..とエラー UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) //の所に Expected declarationとエラー
###該当のソースコード
Swift3
1import UIKit 2import UserNotifications 3 4@UIApplicationMain 5class AppDelegate: UIResponder, UIApplicationDelegate { 6 7 var window: UIWindow? 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 11 // 通知の許可をリクエスト 12 let center = UNUserNotificationCenter.current() 13 center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in 14 if error != nil { 15 return 16 } 17 18 if granted { 19 debugPrint("通知許可") 20 let center = UNUserNotificationCenter.current() 21 center.delegate = self as? UNUserNotificationCenterDelegate 22 } else { 23 debugPrint("通知拒否") 24 } 25 26 }) 27 28 return true 29 30 31 32 33 } 34 35 // 通知オプションのカスタマイズ 36 @available(iOS 10.0, *) 37 func userNotificationCenter(_ center: UNUserNotificationCenter, 38 willPresent notification: UNNotification, 39 withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 40 41 let identifier = notification.request.identifier 42 switch identifier { 43 case "alert": 44 completionHandler([.alert]) // 通知だけ 45 case "both": 46 completionHandler([.sound, .alert]) // サウンドと通知 47 default: 48 () // 何もしない 49 50 } 51 } 52 53 54 55 56 // 通知に対する操作 57 @available(iOS 10.0, *) 58 func userNotificationCenter(_ center: UNUserNotificationCenter, 59 didReceive response: UNNotificationRequest, 60 withCompletionHandler completionHandler: () -> Void) { 61 debugPrint("opened") 62 completionHandler() 63 } 64 65 66 let content = UNMutableNotificationContent() 67 content.title = "Title" 68 content.subtitle = "Subtitle" 69 content.body = "Body" 70 content.sound = UNNotificationSound.default() 71 72 // 5秒後に発火 73 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 74 let request = UNNotificationRequest(identifier: "FiveSecond", 75 content: content, 76 trigger: trigger) 77 // ローカル通知予約 78 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 79 80 81 82 83 } 84 85 86 87 88 89 90 91 92 93func applicationWillResignActive(_ application: UIApplication) { 94 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 95 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 96 } 97 98 func applicationDidEnterBackground(_ application: UIApplication) { 99 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 100 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 101 } 102 103 func applicationWillEnterForeground(_ application: UIApplication) { 104 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 105 } 106 107 func applicationDidBecomeActive(_ application: UIApplication) { 108 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 109 } 110 111 func applicationWillTerminate(_ application: UIApplication) { 112 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 113 } 114 115 116 117 118
###試したこと
エラーコードを昨日から調べているのですが、解決に至っておりません。
ご教授お願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3 Xcode8.3.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。