pushy / pushy-flutter

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

Pushy: Error retrieving handle for Flutter callbacks #40

Closed bw-99 closed 2 years ago

bw-99 commented 2 years ago

Whenever I use Pushy.setNotificationListener(backgroundNotificationListener) method, "Pushy: Error retrieving handle for Flutter callbacks" error appears to me.

So Just in case, I changed my backgroundNotificationListener Function to just print something But same error appears.

pushy commented 2 years ago

Hi @bw-99, Thanks for reaching out. We'd be glad to assist.

1) Please ensure your Flutter Android project has been updated to Flutter 1.12+ for compatibility with the new version of our plugin. Your android/app/src/main/AndroidManifest.xml should contain a <meta-data> attribute labeled flutterEmbedding with a value of 2.

2) Remove the following lines from your android/app/src/main/AndroidManifest.xml:

<!-- Pushy Notification Receiver -->
<!-- Incoming push notifications will invoke the following BroadcastReceiver -->
<receiver android:name="me.pushy.sdk.flutter.PushReceiver" android:exported="false">
    <intent-filter>
        <!-- Do not modify this -->
        <action android:name="pushy.me" />
    </intent-filter>
</receiver>

3) Remove any existing Pushy.setNotificationListener((data) => {}) method from your application.

4) Add the following method to your main.dart file, right after the import statements, and outside any Widget class declaration, to process push notifications in the background via a Flutter background isolate:

// Please place this code in main.dart,
// After the import statements, and outside any Widget class (top-level)

void backgroundNotificationListener(Map<String, dynamic> data) {
    // Print notification payload data
    print('Received notification: $data');

    // Notification title
    String notificationTitle = 'MyApp';

    // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
    String notificationText = data['message'] ?? 'Hello World!';

    // Android: Displays a system notification
    // iOS: Displays an alert dialog
    Pushy.notify(notificationTitle, notificationText, data);

    // Clear iOS app badge number
    Pushy.clearBadge();
}

Feel free to modify this sample code to suit your own needs.

5) Call the Pushy.setNotificationListener(backgroundNotificationListener) method after Pushy.listen() to configure the background notification listener:

// Listen for push notifications received
Pushy.setNotificationListener(backgroundNotificationListener);

Please run your app after performing these changes and let us know if it resolves the issue.