wonderpush / wonderpush-ios-sdk

iOS SDK for WonderPush − This plugin makes it easy to set up WonderPush push notifications on your iOS apps for iPhone, iPad and Apple devices. High volume, fast delivery and full-featured starting €1/month.
https://docs.wonderpush.com/docs/ios-push-notifications-quickstart
Apache License 2.0
5 stars 8 forks source link

Wonderpush and local notification conflict #13

Closed EArminjon closed 3 years ago

EArminjon commented 3 years ago

Hi,

I would like to get some wonderpush notification from my backend and some scheduled ones from my app. I see a conflict, my local notification didn't work as expected. In fact, In my code, I set an action on notification click. It seems that, wonderpush redirect the notification to the right handler (User Notification Framework) but without the gesture. When clicking on my local notification, the app is launched but my callback inside is not called.

My environnement : Flutter (swift, kotlin) wonderpush_flutter 2.1.3 wonderpush_fcm_flutter 1.0.3 flutter_local_notifications 8.1.1+2 (User Notification Framework)

Bellow, logs:

2021-09-06 12:46:00.057619+0200 Runner[770:69810] [WonderPush] Notification is not for WonderPush
2021-09-06 12:46:02.879368+0200 Runner[770:69810] [WonderPush] userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
2021-09-06 12:46:02.879772+0200 Runner[770:69810] [WonderPush] ↵
userNotificationCenter:<UNUserNotificationCenter: 0x282635410> didReceiveNotificationResponse:<UNNotificationResponse: 0x282713f30; actionIdentifier: com.apple.UNNotificationDefaultActionIdentifier, notification: <UNNotification: 0x2827127c0; source: myappforhgithubissue date: 2021-09-06 10:46:00 +0000, request: <UNNotificationRequest: 0x282713000; identifier: 1, content: <UNNotificationContent: 0x2813b6ff0; title: <redacted>, subtitle: (null), body: <redacted>, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x2803f9490>, realert: 0, trigger: <UNCalendarNotificationTrigger: 0x282f15f00; dateComponents: <NSDateComponents: 0x282ab4150> {
    TimeZone: Europe/Paris (CEST) offset 7200 (Daylight)
    Hour: 12
    Minute: 46
    Second: 0
    Weekday: 2, repeats: YES>>, intents: (
)>> withCompletionHandler:

Can you help me :) ?

EArminjon commented 3 years ago

Fixed with the following swift code :

import UIKit
import Firebase
import Flutter
import WonderPush

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    var flutter_native_splash = 1
    UIApplication.shared.isStatusBarHidden = false

    WonderPush.setClientId("the_secret_client_id", secret:"the_secret_secret")

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        //must be call after UNUSErNotificationCenter, else wonderpush is overrided
        WonderPush.setupDelegateForUserNotificationCenter()
    }

    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    return WonderPush.application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ application: UIApplication?, didReceive notification: UILocalNotification?) {
    WonderPush.application(application, didReceive: notification)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    WonderPush.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    WonderPush.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
    WonderPush.application(application, didReceiveRemoteNotification: userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    WonderPush.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    WonderPush.applicationDidEnterBackground(application)
}

func applicationDidBecomeActive(_ application: UIApplication) {
    WonderPush.applicationDidBecomeActive(application)
}

func application(_ application: UIApplication?, didRegister notificationSettings: UIUserNotificationSettings?) {
    WonderPush.application(application, didRegister: notificationSettings)
}
}
ofavre commented 3 years ago

Hi,

I'm not sure what you mean by "the gesture". Is it a field on the UNNotificationResponse that you were expecting? Our SDK does not touch such object. Source

So you basically removed the call to WonderPush.setupDelegateForApplication(application) and forwarded the individual calls yourself. So the only difference I see is that this may be better compatible with any other framework that may affect the AppDelegate too.

Maybe the only change required can be the order of calls to WonderPush.setupDelegateForApplication(application) and/or WonderPush.setupDelegateForUserNotificationCenter().

stephanejais commented 2 years ago

A simpler solution is to slightly change the WonderPush initialization in the AppDelegate:

place it later, in the application:didFinishLaunchingWithOptions: method, right after GeneratedPluginRegistrant.register(with: self)

This change will make sure your handler is called on notification click.

I've updated our Flutter instructions to reflect this: https://docs.wonderpush.com/docs/flutter-push-notifications#step-2-prepare-your-xcode-project-for-push-notifications