Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> iOS

วิธีตรวจสอบสถานะ Notification ของแอป iOS

Notifications แจ้งข้อมูลสำคัญให้ผู้ใช้ ไม่ว่าจะเป็น Alert, Sound, Badge

วิธีตรวจสอบสถานะ Notification ของแอป iOS

อ่านเพิ่มเติม Apple แนะนำใช้ UserNotifications Framework

วิธีตรวจสอบสถานะ

ขั้นตอนที่ 1: Import Framework

import UserNotifications

ขั้นตอนที่ 2: สร้าง Object

let currentNotification = UNUserNotificationCenter.current()

ขั้นตอนที่ 3: ตรวจสอบสถานะ

currentNotification.getNotificationSettings { (settings) in
if settings.authorizationStatus == .notDetermined {
// ยังไม่ได้ขออนุญาต
} else if settings.authorizationStatus == .denied {
// ถูกปฏิเสธ ต้องไป Settings เปิด
} else if settings.authorizationStatus == .authorized {
// อนุญาตแล้ว
}
}

โค้ดเต็ม

import UserNotifications
let currentNotification = UNUserNotificationCenter.current()
currentNotification.getNotificationSettings { (settings) in
if settings.authorizationStatus == .notDetermined {
// Notification permission is yet to be been asked go for it!
} else if settings.authorizationStatus == .denied {
// Notification permission was denied previously
} else if settings.authorizationStatus == .authorized {
// Notification permission already granted.
}
}