pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 19 forks source link

await Pushy.register hangs #20

Closed grantespo closed 3 years ago

grantespo commented 3 years ago

I am using the latest version pushy_flutter: 1.1.7

I am following the docs. Pushy.listen() and Pushy.setNotificationListener(backgroundNotificationListener); seem to work okay, however Pushy.register() hangs.

try {
      print("pushyRegister try{}");
      String deviceToken = await Pushy.register();
      print("pushyRegister deviceToken " + deviceToken);
    } catch (error) {
      print("pushyRegister catch " + error);
    }

In my logcat, I only see "pushyRegister try{}". catch isn't being called. Any ideas of why this could happen.

I am also using Firebase in my project

pushy commented 3 years ago

Hi @grantespo, Thanks for reporting this issue.

We've now added an option to make it possible to use Firebase & Pushy together in the same iOS project.

1) Please edit the pubspec.yaml in the root directory of your project and update the plugin version referenced to the following:

pushy_flutter: 1.1.8

Run flutter pub get.

2) Add the following line of code in your project's lib/main.dart right after Pushy.listen():

// Disable Pushy AppDelegate method swizzling
Pushy.toggleMethodSwizzling(false);

3) Disable Firebase SDK method swizzling by adding the FirebaseAppDelegateProxyEnabled flag to your project's Info.plist file and setting it to NO (boolean value). More info: https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in.

4) Edit your ios/Runner/AppDelegate.swift, adding the following import line and overriding methods:

import Pushy

// APNs has assigned the device a unique token
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Call internal Pushy SDK method
    Pushy.shared?.application(application, didRegisterForRemoteNotificationsWithDeviceToken:deviceToken)

    // Pass token to Firebase SDK
    Messaging.messaging().apnsToken = deviceToken
}

// APNs failed to register the device for push notifications
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Call internal Pushy SDK method
    Pushy.shared?.application(application, didFailToRegisterForRemoteNotificationsWithError:error)
}

// Device received notification (legacy callback)
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    // Call internal Pushy SDK method
    Pushy.shared?.application(application, didReceiveRemoteNotification:userInfo)

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    Messaging.messaging().appDidReceiveMessage(userInfo)
}

// Device received notification (with completion handler)
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Call internal Pushy SDK method
    Pushy.shared?.application(application, didReceiveRemoteNotification:userInfo, fetchCompletionHandler: completionHandler)

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    Messaging.messaging().appDidReceiveMessage(userInfo)
}

5) Run your app and observe whether both the Pushy SDK and Firebase are able to register successfully and receive notifications.