繁体   English   中英

收到后如何更改iOS通知消息?

[英]How to change iOS notification message upon receiving?

当我从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])
}

您无法更改request本身,因为它只是get ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM