私はまだiOSの10での通知を使用していないので、私は学習として先に行って、それを考え出しました私の経験は、今私はあなたにそれを渡すことができます。
UILocalNotification
は、iOS 10では償却され、UserNotifications
フレームワークに置き換えられました。あなたのAppDelegate
通知を表示し、ユーザからの許可を取得し、UNUserNotificationCenterDelegate
でセンターのデリゲートを設定するには
:
アクションの取り扱い
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Play sound and show alert to the user
completionHandler([.alert,.sound])
}
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.delegate = self
let options: UNAuthorizationOptions = [.alert, .sound];
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
print("Something went wrong")
}
}
return true
}
は、ユーザーに通知を提示
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
// Determine the user action
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
print("Dismiss Action")
case UNNotificationDefaultActionIdentifier:
print("Default")
case "foo":
print("foo")
case "bar":
print("bar")
default:
print("Unknown action")
}
completionHandler()
}
これはどこでも実行できますアプリ内のすべての通知のすべてのアクションとカテゴリを設定できます。
func setupActions() {
//create first action
let foo = UNNotificationAction(
identifier: "foo",
title: "foo"
)
//create second action
let bar = UNNotificationAction(
identifier: "bar",
title: "bar",
options: [.destructive]
)
//put the two actions into a category and give it an identifier
let cat = UNNotificationCategory(
identifier: "cat",
actions: [foo, bar],
intentIdentifiers: []
)
//add the category to the notification center
UNUserNotificationCenter.current().setNotificationCategories([cat])
}
そして最後に、実際の通知を作成:彼らは中央ではなく、通知自体に割り当てられているので
func setupNotification() {
let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "A message"
content.sound = UNNotificationSound.default()
//make sure to assign the correct category identifier
content.categoryIdentifier = "cat"
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "hello", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print("theError \(theError)")
}
}
}
これらの機能を活用し、各クラスにimport UserNotifications
に確認してください。
詳細情報:User Notifications documentation
おそらくあなたが探しているもの:http://www.appcoda.com/local-notifications-ios8/ – Frankie
+ Frankie私はそのウェブサイトを試してみましたが、多くのエラーが発生しました – krish
あなたの質問は理にかなっていません。ローカル通知をスケジュールしているのですが、なぜremoteNotificationsをチェックしていますか? – Adeel