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

时间:2017-11-07 18:53:31

标签: ios swift notifications apple-push-notifications onesignal

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

的内容
", you have received a message"

我想在应用中使用类似

的用户名替换$ name
notificationMessage = UserDefaults.standard.string(forKey: "username") + notificationMessage

是否可以覆盖通知?

1 个答案:

答案 0 :(得分:2)

如果您要更改系统显示的提醒,那么您无法更改这些提醒。它们由操作系统管理。

仅限前景:

如果您有一些内部提醒,您想要弹出应用程序位于前台,那么您可以随心所欲地进行操作

例如,您可以执行以下操作:

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 ...

说完我就不建议这样做。您应该在推送这些通知的服务器上处理您的逻辑。这应该是不必要的。