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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

2126閲覧

User Notifications Frameworkの実装について

hik_

総合スコア42

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2017/06/10 06:05

###前提・実現したいこと
リマインダーアプリを作りたく、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

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

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

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

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

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

guest

回答1

0

自己解決

コードを間違って

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

内に書いていました。コードを

func applicationWillResignActive(_ application: UIApplication) {

内に書き解決しました。

投稿2017/06/10 07:46

hik_

総合スコア42

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問