1

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?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Myat Min Soe
  • 787
  • 8
  • 32

1 Answers1

2

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • How to customize Alert Notification text in Background? or it's not doable ? – Mamdouh El Nakeeb Nov 13 '17 at 00:00
  • @MamdouhElNakeeb That's handled by the OS. Not doable. Again best to let the server handle that logic completely. – mfaani Nov 13 '17 at 00:01
  • @MamdouhElNakeeb Akkh al-arab, oghollok! La yumkin! – mfaani Nov 13 '17 at 00:03
  • @MamdouhElNakeeb The moment the OS is about to show it, you can't change, there is no moment where the OS tells you "I'm about to show this, would like to make a change?" However, you can update the content *delivered* notifications (remote or local) or local notifications that will be delivered later. See [here](https://stackoverflow.com/questions/43463175/updating-a-delivered-notifications-ios-10/43463261#43463261). It may be what you need – mfaani Nov 13 '17 at 00:08
  • I need to generate different local notifications depending on the remote notification I get. you got my question? – Mamdouh El Nakeeb Nov 13 '17 at 00:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158817/discussion-between-honey-and-mamdouh-el-nakeeb). – mfaani Nov 13 '17 at 00:15