Notificare / notificare-sdk-android

Notificare's Android SDK
MIT License
3 stars 0 forks source link

🐞: Change of App Key is not detected as change for device registratoin #180

Closed Danielku15 closed 3 weeks ago

Danielku15 commented 4 weeks ago

Is there an existing issue for this?

Describe the bug

We have a multi-tenancy app where the mobile app registers to different apps in the backend. This is done by configuring notificare with AppKey/AppSecret matching the tenant.

When a user switches the tenant, the device doesn't register to the new tenant. When login to a new tenant (changed app key) the device registration is skipped and the launch fails with a 404 when loading the inbox:

https://github.com/Notificare/notificare-sdk-android/blob/f756ff63b5930360bbb43dafcb29dde61b37cb11/notificare/src/main/java/re/notifica/internal/modules/NotificareDeviceModuleImpl.kt#L343-L344 https://github.com/Notificare/notificare-sdk-android/blob/f756ff63b5930360bbb43dafcb29dde61b37cb11/notificare/src/main/java/re/notifica/internal/modules/NotificareDeviceModuleImpl.kt#L389-L458

If the AppKey for the app changed since the last launch, the device must also register newly (any maybe optionally unregister from the old one?)

Steps to reproduce

  1. Disable the automatic configuration via <meta-data android:name="re.notifica.auto_configuration_enabled" android:value="false"/> in your manifest.
  2. Start the app with a first app key configured: Notificare.configure(context, "AppKey1", "AppSecret1") and Notificare.launch()
  3. Test the app and send a push notification, you receive it.
  4. Change the app to have a new app key Notificare.configure(context, "AppKey2", "AppSecret2") and Notificare.launch()
  5. Test the app. Notice in the log errors.

Expected behaviour

The SDK should detect a change in app keys. It first should attempt deleting the registration on the old app it had before. And then it should register freshly in the new app.

It would also be fine for me if we can set somehow the device information before any registration starts. To force a re-registration. e.g. the currentDevice.userId. But Notificare.device().register(...) requires the SDK to already be initialized successfully.

Relevant log output

##
## Successful registration

16:05:36:316    [TextView] onProvideContentCaptureStructure(): calling assumeLayout()
16:05:36:316    [TextView] onProvideContentCaptureStructure(): calling assumeLayout()
16:05:36:403    [shnotifications] Compiler allocated 4849KB to compile void android.view.ViewRootImpl.performTraversals()
16:05:37:292    [WindowOnBackDispatcher] sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda11@8565e1a
16:05:37:345    [HWUI] endAllActiveAnimators on 0xb4000073edf0c420 (AlertController$RecycleListView) with handle 0xb4000074bec8b030
16:05:38:252    [monodroid-assembly] open_from_bundles: failed to load assembly Xamarin.Firebase.Common.dll
16:05:41:424    [Choreographer] Skipped 194 frames!  The application may be doing too much work on its main thread.
16:05:48:673    [CompatibilityChangeReporter] Compat change id reported: 78294732; UID 10369; state: ENABLED
16:05:48:738    [monodroid-assembly] Shared library 'liblog' not loaded, p/invoke '__android_log_print' may fail
16:05:48:738    [DOTNET] Granted
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: events
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: session
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: device
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: crash_reporter
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: push
16:05:48:738    [Notificare] [ServiceManager$Companion] Detected GMS peer dependency. Setting it as the target platform.
16:05:48:738    [Notificare] [NotificarePush] Creating the default notifications channel.
16:05:48:738    [Notificare] [MauiAppCompatActivity] Configuring module: inbox
16:05:48:738    [Notificare] [MauiAppCompatActivity] Checking if there is legacy data that needs to be migrated.
16:05:48:738    [Notificare] [MauiAppCompatActivity] Notificare configured all services.
16:05:48:738    [Notificare] [MauiAppCompatActivity] Launching Notificare.
16:05:48:738    [Notificare] [NotificarePush] Notificare is not ready yet.
16:05:48:797    [okhttp.OkHttpClient] --> GET https://push.notifica.re/application/info
16:05:48:797    [Notificare] [NotificareInbox] Received an inbox live data update.
16:05:48:797    [Notificare] [NotificareInbox] Notificare application is not yet available.
16:05:48:797    [Notificare] [NotificareInbox] Notificare application is not yet available.
16:05:49:870    [TrafficStats] tagSocket(144) with statsTag=0xffffffff, statsUid=-1
16:05:50:072    [okhttp.OkHttpClient] <-- 200 https://push.notifica.re/application/info (1294ms, 697-byte body)
16:05:50:253    [Notificare] [Notificare$launch] Launching module: events
16:05:50:253    [Notificare] [NotificareEvents] Scheduling a worker to process stored events when there's connectivity.
16:05:50:253    [Notificare] [Notificare$launch] Launching module: session
16:05:50:253    [Notificare] [Notificare$launch] Launching module: device
16:05:50:253    [Notificare] [NotificareDevice] New install detected
16:05:50:253    [Notificare] [NotificareDevice] Registration check: fresh installation
16:05:50:253    [okhttp.OkHttpClient] --> POST https://push.notifica.re/device (333-byte body)
16:05:50:253    [okhttp.OkHttpClient] <-- 201 https://push.notifica.re/device (57ms, 44-byte body)
16:05:50:253    [Notificare] [NotificareSession$startSession] Session '########-####-####-####-20c2770dc68b' started at 16:05:50
16:05:50:253    [okhttp.OkHttpClient] --> POST https://push.notifica.re/event (168-byte body)
16:05:50:312    [okhttp.OkHttpClient] <-- 202 https://push.notifica.re/event (56ms, 38-byte body)
16:05:50:312    [Notificare] [NotificareEvents$log] Event 're.notifica.event.application.Open' sent successfully.
16:05:50:312    [Notificare] [ProcessEventsWorker] Finished processing all the events.
16:05:50:312    [WM-WorkerWrapper] Worker result SUCCESS for Work [ id=########-####-####-####-4ff0d88bad3c, tags={ re.notifica.internal.workers.ProcessEventsWorker } ]
16:05:50:312    [okhttp.OkHttpClient] --> POST https://push.notifica.re/event (171-byte body)
16:05:50:402    [okhttp.OkHttpClient] <-- 202 https://push.notifica.re/event (125ms, 38-byte body)
16:05:50:402    [Notificare] [NotificareEvents$log] Event 're.notifica.event.application.Install' sent successfully.
16:05:50:465    [okhttp.OkHttpClient] --> POST https://push.notifica.re/event (176-byte body)
16:05:50:515    [okhttp.OkHttpClient] <-- 202 https://push.notifica.re/event (61ms, 38-byte body)
16:05:50:517    [Notificare] [NotificareEvents$log] Event 're.notifica.event.application.Registration' sent successfully.
16:05:50:517    [Notificare] [Notificare$launch] Launching module: crash_reporter
16:05:50:517    [Notificare] [NotificareCrashReporter] No crash report to process.
16:05:50:517    [Notificare] [Notificare$launch] Launching module: push
16:05:50:517    [Notificare] [NotificarePush] Processing postponed push token during launch.
16:05:50:517    [Notificare] [NotificarePush] Processing a postponed push token before enableRemoteNotifications() has been called.
16:05:50:517    [Notificare] [NotificarePush$updateNotificationSettings] User notification settings update skipped, nothing changed.
16:05:50:517    [Notificare] [Notificare$launch] Launching module: inbox
16:05:50:517    [Notificare] [NotificareInbox$sync] The local inbox contains no items. Checking remotely.
16:05:50:517    [okhttp.OkHttpClient] --> GET https://push.notifica.re/notification/inbox/fordevice/##########################072361?skip=0&limit=100
16:05:50:580    [okhttp.OkHttpClient] <-- 200 https://push.notifica.re/notification/inbox/fordevice/##########################072361?skip=0&limit=100 (63ms, 38-byte body)
16:05:50:646    [Notificare] [NotificareInbox$requestRemoteInboxItems] Done loading inbox items.
16:05:50:646    [Notificare] [Notificare$launch] /==================================================================================/
16:05:50:646    [Notificare] [Notificare$launch] Notificare SDK is ready to use for application
16:05:50:646    [Notificare] [Notificare$launch] App name: #####
16:05:50:646    [Notificare] [Notificare$launch] App ID: #####################911
16:05:50:646    [Notificare] [Notificare$launch] App services: richPush, locationServices, apns, gcm, hms, inAppMessaging, triggers, liveActivity, websitePush, inbox, email, sms
16:05:50:646    [Notificare] [Notificare$launch] /==================================================================================/
16:05:50:646    [Notificare] [Notificare$launch] SDK version: 3.10.0
16:05:50:646    [Notificare] [Notificare$launch] SDK modules: push, inbox
16:05:50:646    [Notificare] [Notificare$launch] /==================================================================================/
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: events
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: session
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: device
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: crash_reporter
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: push
16:05:50:646    [Notificare] [Notificare$launch] Post-launching module: inbox

## 
## Failed Registraiton (second launch)

16:11:04:899    [TextView] onProvideContentCaptureStructure(): calling assumeLayout()
16:11:04:899    [TextView] onProvideContentCaptureStructure(): calling assumeLayout()
16:11:04:973    [shnotifications] Compiler allocated 4849KB to compile void android.view.ViewRootImpl.performTraversals()
16:11:05:787    [WindowOnBackDispatcher] sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda11@badd198
16:11:05:853    [HWUI] endAllActiveAnimators on 0xb4000073edeffc90 (AlertController$RecycleListView) with handle 0xb4000074bec857b0
16:11:09:195    [DOTNET] Granted
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: events
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: session
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: device
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: crash_reporter
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: push
16:11:09:258    [Notificare] [ServiceManager$Companion] Detected GMS peer dependency. Setting it as the target platform.
16:11:09:258    [Notificare] [NotificarePush] Creating the default notifications channel.
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Configuring module: inbox
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Notificare configured all services.
16:11:09:258    [Notificare] [ButtonHandler_ButtonClickListener] Launching Notificare.
16:11:09:317    [Notificare] [NotificarePush] Notificare is not ready yet.
16:11:09:317    [Notificare] [NotificareInbox] Received an inbox live data update.
16:11:09:386    [okhttp.OkHttpClient] --> GET https://push.notifica.re/application/info
16:11:09:386    [Notificare] [NotificareInbox] Notificare auto badge functionality is not enabled.
16:11:10:406    [TrafficStats] tagSocket(138) with statsTag=0xffffffff, statsUid=-1
16:11:10:789    [okhttp.OkHttpClient] <-- 200 https://push.notifica.re/application/info (1457ms, 697-byte body)
16:11:10:851    [Notificare] [Notificare$launch] Launching module: events
16:11:10:851    [Notificare] [NotificareEvents] Scheduling a worker to process stored events when there's connectivity.
16:11:10:851    [Notificare] [Notificare$launch] Launching module: session
16:11:10:851    [Notificare] [NotificareSession$startSession] Session '########-####-####-####-cad21a79c095' started at 16:11:11
16:11:10:851    [okhttp.OkHttpClient] --> POST https://push.notifica.re/event (168-byte body)
16:11:10:908    [okhttp.OkHttpClient] <-- 202 https://push.notifica.re/event (55ms, 38-byte body)
16:11:10:908    [Notificare] [NotificareEvents$log] Event 're.notifica.event.application.Open' sent successfully.
16:11:10:908    [Notificare] [Notificare$launch] Launching module: device
16:11:10:908    [Notificare] [NotificareDevice] Skipping device registration, nothing changed.
16:11:10:908    [Notificare] [Notificare$launch] Launching module: crash_reporter
16:11:10:908    [Notificare] [NotificareCrashReporter] No crash report to process.
16:11:10:908    [Notificare] [Notificare$launch] Launching module: push
16:11:10:971    [Notificare] [NotificarePush$updateNotificationSettings] User notification settings update skipped, nothing changed.
16:11:10:971    [Notificare] [Notificare$launch] Launching module: inbox
16:11:10:971    [Notificare] [NotificareInbox$sync] The local inbox contains no items. Checking remotely.
16:11:10:971    [okhttp.OkHttpClient] --> GET https://push.notifica.re/notification/inbox/fordevice/##########################072361?skip=0&limit=100
16:11:10:971    [Notificare] [ProcessEventsWorker] Finished processing all the events.
16:11:10:971    [WM-WorkerWrapper] Worker result SUCCESS for Work [ id=########-####-####-####-82682fd2d88b, tags={ re.notifica.internal.workers.ProcessEventsWorker } ]
16:11:11:035    [okhttp.OkHttpClient] <-- 404 https://push.notifica.re/notification/inbox/fordevice/##########################072361?skip=0&limit=100 (50ms, 28-byte body)
16:11:11:035    [Notificare] [Notificare$launch] Failed to launch 'inbox': re.notifica.internal.network.NetworkException$ValidationException: Unexpected status code '404'.
16:11:11:035    [Notificare] [Notificare$launch] Failed to launch Notificare.
16:11:11:035    [Notificare] re.notifica.internal.network.NetworkException$ValidationException: Unexpected status code '404'.
16:11:11:035    [Notificare]    at re.notifica.internal.network.request.NotificareRequest.handleResponse(NotificareRequest.kt:116)
16:11:11:035    [Notificare]    at re.notifica.internal.network.request.NotificareRequest.access$handleResponse(NotificareRequest.kt:26)
16:11:11:035    [Notificare]    at re.notifica.internal.network.request.NotificareRequest$response$2$1.onResponse(NotificareRequest.kt:57)
16:11:11:035    [Notificare]    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
16:11:11:035    [Notificare]    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
16:11:11:035    [Notificare]    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
16:11:11:035    [Notificare]    at java.lang.Thread.run(Thread.java:1012)

Library version

3.10.0

Operating system

Android 14

Smartphone model

Google Pixel 6 Pro

Additional context

Danielku15 commented 4 weeks ago

NOTE: Another usecase for this change can be a real change of the users from an old to a new app. as part of a software update you might change the AppKey and as people upgrade they should be migrated to the new app.

NOTE2: The iOS SDK has the same behavior https://github.com/Notificare/notificare-sdk-ios/blob/main/NotificareKit/Sources/Internals/Modules/NotificareDeviceModuleImpl.swift#L710

hpinhal commented 3 weeks ago

Hi @Danielku15,

This behaviour is intended by design. It helps prevent side effects when developers make mistakes when changing their keys. What you're proposing is a feature request. We should move this discussion to our support portal so you can share more details about your project. Can you message our support channel?

Danielku15 commented 3 weeks ago

I've sent out a message describing all our needs and what concrete changes we would need from the SDKs to be able to use Notificare for our future push notification needs.