'I can't get a FCM click action to work on IOS PWA
The notifications clicked work fine on both android (installed PWA) and chrome but in ios, I haven't had success, At the moment we set the "category" parameter in the payload and the additional actions on the notifications but I have not been able to perform a clicked when the notification is clicked. Is there a way to send actionable notifications to iOS devices?
Sample JSON of message payload:
{
"to": "cadyI6R8vE27vTOfjE157N:APA91bFIEkJqcHzHex9qOOB3eDGWnKg8Fv74iTgDcJDsUVha81hdtphy1fnPDa7rFlRcK48L8vJkthzOp3NyEd_cvSbOcJ4eI335K2N9I-E8ZfA85_2_OnP02OO8vfGv0Gxzz2ACtfSF",
"collapse_key":"type_a",
"notification":{
"title":"Le Soir d\u2019Alg\u00e9rie",
"body":"Justice : Le proc\u00e8s de Chakib Khelil pr\u00e9vu aujourd\u2019hui",
"icon":"/images/favicon.png",
"click_action":"https://www.lesoirdalgerie.com/actualites/les-graves-accusations-de-benbahmed-80545",
"sound": "default"
},
"apns":{
"payload":{
"aps":{
"category":"GENERAL"
}
}
}
}
Here is my AppDelegatecode :
import UIKit
import FirebaseCore
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// TODO: if we're using Firebase, uncomment this
FirebaseApp.configure()
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
}
}
let generalCategory = UNNotificationCategory(identifier: "GENERAL",
actions: [],
intentIdentifiers: [],
options: .customDismissAction)
// Register the category.
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([generalCategory])
Messaging.messaging().subscribe(toTopic: "allDevices") { error in
print("Subscribed to weather topic")
}
// [END register_for_notifications]
return true
}
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID 1: \(messageID)")
}
// Print full message.
print("push userInfo 1:", userInfo)
print("TAB")
sendPushToWebView(userInfo: userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID 2: \(messageID)")
}
// Print full message. **
print("push userInfo 2:", userInfo)
print("TAB")
sendPushToWebView(userInfo: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
// [END receive_message]
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the FCM registration token.
// func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// print("APNs token retrieved: \(deviceToken)")
//
// // With swizzling disabled you must set the APNs token here.
// // Messaging.messaging().apnsToken = deviceToken
// }
}
// [START ios_10_message_handling]
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: 3 \(messageID)")
}
// Print full message.
print("push userInfo 3:", userInfo)
print("ghiii");
var json = "";
do {
let jsonData = try JSONSerialization.data(withJSONObject: userInfo)
json = String(data: jsonData, encoding: .utf8)!
print(json);
let t = userInfo["aps"] as? [String: Any];
if let h = t?["category"] as! String? {
print(h);
if let url = URL(string: h) {
UIApplication.shared.open(url)
}
}
} catch {
print("ERROR: userInfo parsing problem")
}
sendPushToWebView(userInfo: userInfo)
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID 4: \(messageID)")
}
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print("push userInfo 4:", userInfo)
sendPushToWebView(userInfo: userInfo)
completionHandler()
}
}
// [END ios_10_message_handling]
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")
let dataDict:[String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
// [END refresh_token]
}
Any help would be greatly appreciated since I have not found a single thing that would help me understand this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
