Closed zarko-tg closed 6 months ago
@zarko-tg I apologize for such a delayed response. I'll look into the @capacitor/local-notifications plugin and try to create an example project for you.
Stay tuned!
@wfhm That'd be fantastic.
@zarko-tg Okay, so there is a workaround but it is quite complicated, and our standard approach to resolving delegate conflicts is not applicable.
Right now, if you disable Pushwoosh notification delegate directly in the plugin, you are still left with Capacitor delegate overwriting your app's delegate during CapacitorBridge initialization, and it happens when Capacitor core is initialized, not just the plugin. So we cannot just create a custom router delegate that will distribute notifications between Pushwoosh and local-notifications plugin without messing with Capacitor initialization.
The solution I ended up with is creating a helper class inside the local-notifications plugin that will receive pushes and forward it to Pushwoosh handler methods.
First, add Pushwoosh XCFramework to your Xcode project pods, for example, using Swift Package Manager. You can follow the first part of this guide: https://docs.pushwoosh.com/platform-docs/pushwoosh-sdk/ios-push-notifications/setting-up-pushwoosh-ios-sdk/swift-package-manager-setup#integration
Add the framework to Pods/CapacitorLocalNotifications target:
Replace its code with the code of PushwooshNotificationHandler.swift
from the attached archive
Then, we need to modify the local-notifications plugin code to use this class as push notifications handler. Replace the code of LocalNotificationsPlugin.swift
with the one you can find in the attached archive.
Finally, disable Pushwoosh notification center delegate in the Pushwoosh plugin by removing (or commenting out) this line:
[UNUserNotificationCenter currentNotificationCenter].delegate = [PushNotificationManager pushManager].notificationCenterDelegate;
That should be enough to make Capacitor's NotificationRouter
class serve as a single delegate in your app that can also receive Pushwoosh notifications. However, for some reason, received push notifications generate a local notification with the same content, so, to work around it, you can also modify the the code of Capacitor/NotificationRouter class by adding the following check to the willPresent:withCompletionHandler
method:
public func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let presentationOptions: UNNotificationPresentationOptions?
if notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true {
presentationOptions = pushNotificationHandler?.willPresent(notification: notification)
} else {
if let userInfo = notification.request.content.userInfo["pw_push"] {
return
}
presentationOptions = localNotificationHandler?.willPresent(notification: notification)
}
completionHandler(presentationOptions ?? [])
}
You can also find its code in the archive below.
We are planning to rework delegate swizzling in our plugins to make resolving such conflicts much easier, so I will keep this issue open until there is any news.
@zarko-tg The conflict was fixed in the new version of the plugin: https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/releases/tag/8.3.17
Please feel free to message us, if you have any follow-up questions.
Hi @wfhm, Firstly, apologies for not being able to check the workaround due to time constraints. I'll be checking out the new version for iOS but we now get an (unrelated?) Android failure:
> Task :app:processReleaseMainManifest FAILED
/Users/foo/bar/android/app/src/main/AndroidManifest.xml Error:
uses-sdk:minSdkVersion 23 cannot be smaller than version 26 declared in library [com.pushwoosh:pushwoosh-inbox-ui:6.7.0] /Users/foo/.gradle/caches/transforms-3/0f75b005e76e163215622787ea7bc654/transformed/pushwoosh-inbox-ui-6.7.0/AndroidManifest.xml as the library might be using APIs not available in 23
Suggestion: use a compatible library with a minSdk of at most 23,
or increase this project's minSdk version to at least 26,
or use tools:overrideLibrary="com.pushwoosh.inbox.ui" to force usage (may lead to runtime failures)
See https://developer.android.com/r/studio-ui/build/manifest-merger for more information about the manifest merger.
FAILURE: Build failed with an exception.
I don't think we can move (so high) from 23 for the time being. And we don't even use pushwoosh-inbox-ui. What'd be the right thing to do about it?
@zarko-tg please let me check if we can downgrade minSdkVersion and I'll get back to you shortly.
@zarko-tg Please try the 8.3.18 release - minSdkVersion was reverted back to 23.
@zarko-tg Small update. Please, try the 8.3.19 release.
Thanks for that, it seems to have solved the problem!
Basically, looking at the surface of things, having the Pushwoosh plugin running next to the @capacitor/local-notifications makes the listeners of the latter stop working on Capacitor iOS. @capacitor/local-notifications "localNotificationReceived" & "localNotificationActionPerformed" are kinda important from a standpoint of a user/UI flow so I'm hoping this can get fixed or worked around in either of the plugins.
As wfhm put it nicely in https://github.com/Pushwoosh/pushwoosh-ios-sdk/issues/94#issuecomment-1706602867 :
A reproduction code/project is available with the comment here: https://github.com/Pushwoosh/pushwoosh-ios-sdk/issues/94#issuecomment-1693204785