pushy / pushy-flutter

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

notification received in console, but notification not appear on device #35

Closed sgarchavada closed 2 years ago

sgarchavada commented 2 years ago

pushy_flutter: ^2.0.1

Here's my implementation

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SentryFlutter.init(
    (options) => options.dsn = 'https://whaterver@o0.sentry.io/0',
    appRunner: () => runApp(
      Phoenix(
        child: AppCore(),
      ),
    ),
  );
}

class _AppCoreState extends State<AppCore> {
  @override
  void initState() {
    super.initState();
    Pushy.listen();
    initClearNotificationsState();

    Pushy.setNotificationClickListener((Map<String, dynamic> data) {
      print('Notification click: $data');

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

here's the log I am receiving when I send push to this device. Screen Shot 2021-11-26 at 2 04 28 PM

But notification is not appearing

I tried another demo, its working fine. but not in my real app. is this because I am wrapping this implementation in sentry?

Only tested this android.

Also Its say Invoking notification listener in foreground (no isolate), though my app is in background state.

pushy commented 2 years ago

Hi @sgarchavada, Please ensure you implemented the backgroundNotificationListener method, as it is missing from your implementation above.

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();
}

Check out the Pushy Flutter Demo main.dart for reference.