0
0
iOS Swiftmobile~10 mins

Push notifications (APNs + FCM) in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to request user permission for push notifications in Swift.

iOS Swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, [1]]) { granted, error in
    if granted {
        print("Permission granted")
    }
}
Drag options to blanks, or click blank then click option'
A.calendar
B.location
C.badge
D.contacts
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing unrelated options like location or contacts which are not notification permissions.
2fill in blank
medium

Complete the code to register for remote notifications after permission is granted.

iOS Swift
DispatchQueue.main.async {
    UIApplication.shared.[1]()
}
Drag options to blanks, or click blank then click option'
AregisterUserNotificationSettings
BregisterForRemoteNotifications
CbeginBackgroundTask
DopenSettingsURLString
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated or unrelated methods like registerUserNotificationSettings.
3fill in blank
hard

Fix the error in the delegate method to receive the device token for APNs.

iOS Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken [1]: Data) {
    let tokenParts = [2].map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    print("Device Token: \(token)")
}
Drag options to blanks, or click blank then click option'
AdeviceToken
BnotificationToken
CtokenData
DpushToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names causes the delegate method not to be called.
4fill in blank
hard

Fill both blanks to create a dictionary payload for Firebase Cloud Messaging (FCM) with a notification title and body.

iOS Swift
let message: [String: Any] = [
    "notification": [
        "title": [1],
        "body": [2]
    ]
]
Drag options to blanks, or click blank then click option'
A"Hello"
B"Welcome to the app!"
C"Goodbye"
D"Error occurred"
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-string values or forgetting quotes around strings.
5fill in blank
hard

Fill all three blanks to handle receiving a push notification in the app delegate and print its content.

iOS Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.[1].request.content.[2]
    print("Received notification with info: \(userInfo)")
    completionHandler([3])
}
Drag options to blanks, or click blank then click option'
Adelegate
Bcontent
C[.alert, .sound]
DuserInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names or forgetting to call the completion handler.