baseConnectNotification method

Future baseConnectNotification(
  1. BuildContext context,
  2. String ntfUrl
)

Implementation

Future baseConnectNotification(BuildContext context, String ntfUrl) async {
  state.loading = false;
  refresh();
  try {
    state.ntfUrl = ntfUrl;
    refresh();
    var permission = await getPermissionNotification(
      context,
    );
    state.allowedRoutes = [];
    refresh();
    state.allowedRoutes = List.from(permission['allowed_routes']);
    refresh();
    state.allowedRoutes?.add(':private');
    refresh();
    state.sendAllowedRoutes = List.from(permission['allowed_routes']);
    refresh();
    var register = await registerForGetToken(context, formValue: permission);
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('notify_token', register['token']);
    await subscribeBatch(context);
    await unStash(context);

    var myStream = Sse.connect(
        uri: Uri.parse(
            '${state.ntfUrl}sse?token=${prefs.getString('notify_token')}'),
        types: state.allowedRoutes,
        closeOnError: true,
        withCredentials: false,
        reConnect: () {
          baseConnectNotification(context, state.ntfUrl ?? '');
        }).stream;
    myStream.listen((event) {
      if (event.toString().startsWith('{')) {
        Map valueMap = json.decode(event);
        if (!valueMap.containsKey("update")) {
          _showNotification(
              title: valueMap.keys.toString(),
              body: valueMap.values.toString());
        }
      } else {
        _showNotification(title: event.toString(), body: '');
      }
    });

    // var tem = SSEClient.subscribeToSSE(
    //   url: '${Conf.NOTIFICATION_BASE_URL}sse?token=${prefs.getString('notify_token')}',header: {},
    //   method: SSERequestTypes.GET ,
    // ).listen((event) {
    //   _showNotification(title: event.data, body: '');
    // });
    // Timer.periodic(Duration(seconds: 6), (timer) {
    //   tem.cancel();
    //    tem = SSEClient.subscribeToSSE(
    //        method: SSERequestTypes.GET,
    //       url: '${Conf.NOTIFICATION_BASE_URL}sse?token=${prefs.getString('notify_token')}',header: {}).listen((event) {
    //     _showNotification(title: event.data,body: '');
    //   });
    // });
  } catch (e) {
    print('eBaseConnectNotification ===> $e');
  } finally {
    state.loading = true;
    refresh();
  }
}