Swift如何在系统启动之前更改从firebase(FCM)收到的通知标题和正文?

Posted

技术标签:

【中文标题】Swift如何在系统启动之前更改从firebase(FCM)收到的通知标题和正文?【英文标题】:Swift How to change the notification title and body received from firebase (FCM) before fire it up by the system? 【发布时间】:2019-07-31 10:54:23 【问题描述】:

如何在系统启动之前更改收到的 Firebase 通知的标题和正文?

我成功收到通知,但我需要在显示通知之前修改标题和正文

这是我的AppDelegate 课程

extension AppDelegate : UNUserNotificationCenterDelegate 

// Receive displayed notifications for ios 10 devices.
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: \(messageID)")
    

    // Print full message.
    print(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: \(messageID)")
    

    // Print full message.
    print(userInfo)

    completionHandler()



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: \(messageID)")
    

    // Print full message.
    print(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: \(messageID)")
    

    // Print full message.
    print(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)

【问题讨论】:

找到解决方案了吗? 【参考方案1】:

这看起来类似于这里提出的问题:It's possible to change push notification message before display on device from iOS side?

在通知服务中看起来可能是可行的,但是,您也可以按照您希望写入的方式从 FCM 发送有效负载。是否有无法从服务器修改它的原因?

【讨论】:

以上是关于Swift如何在系统启动之前更改从firebase(FCM)收到的通知标题和正文?的主要内容,如果未能解决你的问题,请参考以下文章

Swift:如何在实例化 viewController 之前加载 Firebase 数据?

Swift 从用户向用户发送推送通知 Firebase

在 iOS 上使用 Swift 多次调用 Firebase 'Observe'

Swift 无法识别 Firebase 用户

如何更改启动时显示的默认 SKScene

如何更改 Firebase 实时数据库中子值的顺序