mirzemehdi / KMPNotifier

Kotlin Multiplatform Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)
http://mirzemehdi.com/KMPNotifier/
Apache License 2.0
340 stars 19 forks source link

How can I track the opening of app by clicking on a notification? #19

Closed GimazDo closed 7 months ago

GimazDo commented 7 months ago

Hi, how can i detect that user clicked on notification?

I need that when I click on a notification, a screen will open depending on the data in the notification.

Abdelrhman-sbs commented 7 months ago

@ValdZX @mirzemehdi Please, any help?

mirzemehdi commented 7 months ago

I think this is more about adding deeplink support to library. But I am still checking my options

mirzemehdi commented 7 months ago

@GimazDo @Abdelrhman-sbs

This is implemented in library version 0.5.0.

This is how you can use it:

Detecting notification click and get payload data

NotifierManager.addListener(object : NotifierManager.Listener {
    override fun onNotificationClicked(data: PayloadData) {
        super.onNotificationClicked(data)
        println("Notification clicked, Notification payloadData: $data")
    }
}) 

You need to call below platform-specific functions in order to receive payload data properly.

Android

Call NotifierManager.onCreateOrOnNewIntent(intent) on launcher Activity's onCreate and onNewIntent methods.

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
      NotifierManager.onCreateOrOnNewIntent(intent)
      ...
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        NotifierManager.onCreateOrOnNewIntent(intent)
    }
iOS

Call NotifierManager.onApplicationDidReceiveRemoteNotification(userInfo: userInfo) on application's didReceiveRemoteNotification method.

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
      NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)
      return UIBackgroundFetchResult.newData
 }

For more detailed information you can check PR https://github.com/mirzemehdi/KMPNotifier/pull/20

mirzemehdi commented 7 months ago

Please, let me know if there is any issue

emanuelecastelli commented 7 months ago

On my side (ios impl) onNotificationClicked callback not get called when app is in background. I have followed all the conf steps and tried to use UNUserNotificationCenterDelegate too, but without luck. Can you confirm that on your side package callback is triggered when app is in background?

mirzemehdi commented 7 months ago

@emanuelecastelli yes, when I click notification I get listener callback.

1) Make sure you enabled remote-notificaiton in backgorund modes in XCode. 2) You added this part in ios part

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
        NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)
        return UIBackgroundFetchResult.newData
    }
emanuelecastelli commented 7 months ago

i have done everything. When app is in foreground, i din't see the notification, but i get every log that i do in native and in cross. When app is in background, i see the notification at the top, i tap on it but no log is printed out (nor from native nor from cross). The debugger too not get called in xcode (breakpoint set in didReceiveRemoteNotification). App simply returns in foreground.

emanuelecastelli commented 7 months ago

@mirzemehdi can you provide json body that you use for test push notifications? I'm using something like that, playing with apns props, but no way :(

{
  "message": {
    "token": ".......",
     "notification": {
      "title": "title",
      "body": "Notification from FCM"
    },
    "apns": {

        "headers": {

        },
        "payload": {

        }
    }
  }
}
emanuelecastelli commented 7 months ago

Ok, restoring FCM method swizzling resolve the issue: push notifications callbacks now runs correctly