pushy / pushy-flutter

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

setNotificationClickListener not working in IOS #34

Closed pkphone closed 2 years ago

pkphone commented 3 years ago

I need to listen to the Push notification when a user clicking on it. So I used setNotificationClickListener to listen on user click, but unfortunately, I am not getting any response on it.

pushy commented 3 years ago

Hi @pkphone, We'd be glad to assist.

1) Where are you invoking setNotificationClickListener()? Please ensure it is inside initState(), as implemented here. Also, please share your lib/main.dart, if possible.

2) Please clone and run our Pushy Flutter Demo app and check whether the notification click listener is working there. This will rule out a device-related or Xcode-related issue.

pkphone commented 3 years ago

I put this setNotificationClickListener inside home_widget.dart initState().

It shows notification dialog and then I press the ok button it dismisses the dialog and does not call other code.

Below is the home_widget.dart

@override void initState() { super.initState(); _initPlatformState(); }

Future _initPlatformState() async { Pushy.listen();

Pushy.setNotificationIcon('ic_notify');

try {
  String deviceToken = await Pushy.register();

  debugPrint('Device token: $deviceToken');

  if (mounted) {
    setState(() {
      _deviceToken = deviceToken;
      _instruction =
          Platform.isAndroid ? '(copy from logcat)' : '(copy from console)';
    });
  }
} on PlatformException catch (error) {
  debugPrint('Error: ${error.message}');

  if (mounted) {
    setState(() {
      _deviceToken = 'Registration failed';
      _instruction = '(restart app to try again)';
    });
  }
}

Pushy.setNotificationListener(backgroundNotificationListener);

Pushy.setNotificationClickListener((Map<String, dynamic> data) {
  Pushy.clearBadge();

  RouteUtil.changeRoute<HomeModule>(HomeRoute.notification, args: data);
});

}

Below is the main.dart

void main() async { WidgetsFlutterBinding.ensureInitialized(); await EasyLocalization.ensureInitialized();

await Hive.initFlutter(); registerAdapters(); runApp( EasyLocalization( supportedLocales: [Locale('en'), Locale('mm'), Locale('mmu')], path: 'assets/translations', fallbackLocale: Locale('en'), child: ModularApp( module: AppModule(), child: AppWidget(), ), ), ); }

void backgroundNotificationListener(Map<String, dynamic> data) async { NotiStore notiStore = Modular.get();

if (Platform.isAndroid) { NotiMessage notiMessage = NotiMessage.fromJson(jsonDecode(data['__json'])); await notiStore.saveToNotiMessage(notiMessage); } else if (Platform.isIOS) { NotiMessage notiMessage = NotiMessage(); notiMessage.title = data['title']; notiMessage.message = data['message']; notiMessage.image = data['image']; notiMessage.requestId = data['request_id']; notiMessage.requestType = data['request_type']; notiMessage.slug = data['slug']; await notiStore.saveToNotiMessage(notiMessage); }

String notificationTitle = Strings.appName;

String notificationText = data['message'] ?? '';

Pushy.notify(notificationTitle, notificationText, data);

Pushy.clearBadge(); }

pushy commented 3 years ago

Hi @pkphone, At this time, Pushy.setNotificationListener() and Pushy.setNotificationClickListener() must be executed directly from within your lib/main.dart, and no sub-widget files.

Please move this method to lib/main.dart's initState() method, and give it another try.

pushy commented 2 years ago

Closing due to lack of response, please feel free to comment if you still need help with this issue.