I implemented push notifications earlier using this code. Here is the code I used to push a notification in my app. I did maintain all the procedure as explained. This code triggers the push notification successfully.
private func initPushNotification(_ application: UIApplication) {
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {status, error in
PushNotificationHelper.shared.sendStatus(status)
})
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
UIApplication.shared.applicationIconBadgeNumber = 0
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler([.alert, .sound, .badge])
}
// On Click Notification bar
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
PushNotificationHelper.shared.handlePushNotification(response)
completionHandler()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
#if DEBUG
PushNotificationHelper.shared.subscribeToTopic(topic: PNServiceTopicKey.debug_all.keyValue)
PushNotificationHelper.shared.unsubscribeToTopic(topic: PNServiceTopicKey.release_all.keyValue)
PushNotificationHelper.shared.handleDebugPremiumUser()
#else
PushNotificationHelper.shared.subscribeToTopic(topic: PNServiceTopicKey.release_all.keyValue)
PushNotificationHelper.shared.unsubscribeToTopic(topic: PNServiceTopicKey.debug_all.keyValue)
PushNotificationHelper.shared.handleReleasePremiumUser()
#endif
}
But now I want to show the content in my notification. So I did implemented the Notification Content Extension. But still the content is not showing. This only shows the notification with title and body.
func notificationCenter(){
let notification = UNUserNotificationCenter.current()
notification.requestAuthorization(options : [.alert , .badge , .sound]) { success, error in
if success {
print("Authorization Successful")
notification.delegate = self
}else{
print("Authorization Failed")
}
}
let openButton = UNNotificationAction(identifier: "openButton", title: "Open" , options: .foreground)
let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [openButton], intentIdentifiers: [] , options: [])
notification.setNotificationCategories([category])
}
I am using this payload -
{
"to" : "/topics/avm_ios_debug_all",
"content_available": true,
"category": "content_added_notification",
"alert" : {
"title" : "Try our new photo editor",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "Make your images attractive and colorful",
"data": {
"title": "Try our new photo editor",
"body" : "Make your images attractive and colorful",
"app-url": "https://apps.apple.com/us/app/example",
"deep-link": "deeplink://",
"image" : "https://www.cyberclick.net/hs-fs/hubfs/Banner%20Ads.jpg?width=1200&name=Banner%20Ads.jpg"
}
}
I solved the problem by following the below steps :