ios - 如何在收到时更改 iOS 通知消息?

标签 ios swift notifications apple-push-notifications onesignal

当我从 OneSignal 推送通知时,我想推送类似的东西

", you have received a message"

我想用类似的用户名替换应用程序中的 $name

notificationMessage = UserDefaults.standard.string(forKey: "username") + notificationMessage

是否可以覆盖通知?

最佳答案

如果您打算更改系统显示的警报,那么不,您不能更改这些。它们由操作系统管理。

仅适用于前景:

如果您有一些想要弹出的内部警报——当应用程序处于前台时,您可以随心所欲

例如你可以这样做:

func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    //1. extract notification data
    let message = notification.request.content.body
    let title = notification.request.content.title


    // 2. use the message and title and change their values
    // 3. use your new message and title and show your own custom alert. 


    // 4. I excluded the alert so you could show whatever you like yourself. But still I want to increase the badge and have sound when notification arrives...
    completionHandler([.badge, .sound])
}

您不能更改请求 本身,因为它只是一个get...

话虽如此,我不建议这样做。您的逻辑应该在您推送这些通知的服务器上处理。这应该是不必要的。

关于ios - 如何在收到时更改 iOS 通知消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47165457/

相关文章:

ios - NSLog 输出消息在控制台中重复两次(重复)

iphone - 单例更改不会保存到下一个 View IOS

ios - 如何将 UITableViewCell 定位到 TableView 的底部?

node.js - 当机器人在 Teams 中发送消息时,如何收到通知?

ios - 选择单元格或滚动时 UITableView 上的 UIImage 移动

ios - 来自 RTF 使用 iOS 动态类型的 NSAttributedString

ios - UITextView 数据快速变化

Swift:如何定义具有无主(不安全)引用的 UIView 委托(delegate)?

ios - Swift:不调用 didReceiveRemoteNotification()

android - 无法使用警报管理器取消待定 Intent