यहाँ कैसे मैं अपने AppDelegate यह करने के लिए फ़ाइल की स्थापना की है :
import AudioToolbox
करें कि आपके AppDelegate एक UNUserNotificationCenterDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
अपने "didFinishLaunchingWithOptions" में इस जोड़ें:
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted) {
UIApplication.shared.registerForRemoteNotifications()
} else{
print("Notification permissions not granted")
}
})
यह यदि उपयोगकर्ता पहले से कहा है कि अपने app सूचनाएं भेज सकते हैं का निर्धारण करेगा। यदि नहीं, तो इसे कैसे संभालें इसे संभालें।
एक बार यह पंजीकृत है डिवाइस टोकन तक पहुँच प्राप्त करने के लिए:
//Completed registering for notifications. Store the device token to be saved later
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
self.deviceTokenString = deviceToken.hexString
}
hexString एक विस्तार मैं अपने प्रोजेक्ट में जोड़ी है:
extension Data {
var hexString: String {
return map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
}
}
को संभालने के लिए क्या होता है जब आपके एप्लिकेशन को वह सूचना प्राप्त करता है अग्रभूमि में:
//Called when a notification is delivered to a foreground app.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
//This will get the text sent in your notification
let body = notification.request.content.body
//This works for iphone 7 and above using haptic feedback
let feedbackGenerator = UINotificationFeedbackGenerator()
feedbackGenerator.notificationOccurred(.success)
//This works for all devices. Choose one or the other.
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
}
आपके ऐप को प्राप्त होने पर क्या होता है इसे संभालने के लिए पृष्ठभूमि में अधिसूचना, जिसके बाद उपयोगकर्ता अधिसूचना पर दबाता है:
//Called when a notification is delivered to a background app.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
//Handle the notification
print("did receive")
let body = response.notification.request.content.body
completionHandler()
}