Myat Min Soe
Myat Min Soe

Reputation: 807

How to change iOS notification message upon receiving?

When I push notification from OneSignal, I want to push something like

", you have received a message"

I want to replace $name in app with the username something like

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

Is it possible to override notification?

Upvotes: 1

Views: 2691

Answers (1)

mfaani
mfaani

Reputation: 36347

If you mean to change the alert that the System shows, then NO you can't change those. They are managed by the OS.

For foreground only:

If you have some internal alert that you'd like to pop—when the app is in foreground then you're free to do as you wish

For example you could do something like:

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])
}

you can't change the request itself since it's a get only...

Having that said I don't suggest this. Your logic of this should be handled on the server you push these notifications. This should be unnecessary.

Upvotes: 2

Related Questions