Closed calvinmuller closed 3 years ago
Hi @calvinmuller,
It appears your project has been updated to version 1.1.1
of the pushy_flutter
Dart package. This new version invokes your notification listener in the background, even if your app is not running, allowing you to generate a notification from Dart code.
To update your project to the new Pushy Flutter SDK:
1) Install version 1.1.1
of our Flutter SDK by editing the pubspec.yaml
in the root directory of your project and adding the following under the dependencies section:
pushy_flutter: 1.1.1
Run flutter pub get
to fetch the dependency.
Note: Please ensure your Flutter Android project has been updated to Flutter 1.12+
for compatibility with this 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 let us know if it resolves the issue.
Closing due to lack of response, feel free to comment to reopen.
@pushy-me I am noticing the same error with Flutter 2.0.6 and pushy_flutter: 2.0.1. I am following instructions from the docs, the only difference is, I am using awesome_notifications instead of the default provided by Pushy. Any idea why this could be happening?
Hi @gs-suresh, What error are you seeing? Is it the following?
Isolate / notification callback IDs are missing from SharedPreferences
In that case, make sure you are calling Pushy.setNotificationListener(backgroundNotificationListener);
and passing in a global function defined in top-level main.dart
:
// 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();
}
Thanks @pushy-me. It turned out that the function I was using was not a global one. Switching it to a global function fixed the problem for me.
@gs-suresh Glad to hear! 😄
We recently started getting this and nothing has changed on our app.
Flutter (Channel stable, 1.20.4, on Mac OS X 10.15.5 19F101, locale en-ZA)
Any ideas?