pusher/pusher-channels-flutter

Pusher not updating on Android

Closed this issue · 12 comments

Using pusher on web works perfectly, however when I build on Android the new event dont get updated.

Screenshot (100)
Screenshot (101)

Could you share the code in use when this error is encountered?

Screenshot (111)
Screenshot (106)
Screenshot (107)
Screenshot (108)
Screenshot (109)
Screenshot (110)

These are the screenshots.

I get this error on mobile when the "onEvent" is called.

Screenshot (112)

Screenshot (113)

Another image

There seems to be a couple issues, one around the receipt of events (using onEvent) and another around triggering of events (pusher.trigger). What stands out to me is that both errors seem to include null values.

Are you able to log out parameters to ensure they are as expected?
Could you also share any errors/code going forward as text? I can't copy/paste for replication, and it is difficult to consume code via screenshots.

import 'package:flutter/foundation.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart';
import 'package:get/get.dart';
import 'package:pusher_channels_flutter/pusher_channels_flutter.dart';
import 'package:crypto/crypto.dart'; // for the utf8.encode method
import 'package:safedeals/Ui/Screens/Chat/chat_box.dart';
import 'package:safedeals/Ui/Screens/Chat/funct.dart';
import 'package:safedeals/Ui/widgets.dart';
import 'dart:convert';
import 'package:safedeals/getx_controller.dart';

import '../../../../database/auth_funts.dart';
import '../../../../notification.dart';

PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();
String latestSenderTime =
    ''; //HOLDS THE TIME OF THE LAST MSSG SENT BY THE SENDER. HELPS US FILTER OUT DUPLICATES

class PusherConfig {
  static const appID = "AppID";
  static const key = 'Key';
  static const secret = 'sceretKey';
  static const cluster = 'eu';
  static const hostEndPoint = '';
  static const hostAuthEndPoint = '';
  static const port = '';
}

Future initializePusherAPI(String channelSessionID) async {
  try {
    await pusher.init(
      apiKey: PusherConfig.key,
      cluster: PusherConfig.cluster,
      onConnectionStateChange: onConnectionStateChange,
      onError: onError,
      onSubscriptionSucceeded: onSubscriptionSucceeded,
      onEvent: onEvent,
      onAuthorizer: onAuthorizer,
      onSubscriptionError: onSubscriptionError,
      onDecryptionFailure: onDecryptionFailure,
      onMemberAdded: onMemberAdded,
      onMemberRemoved: onMemberRemoved,
      onSubscriptionCount: onSubscriptionCount,
    );
    await pusher.subscribe(channelName: '$_c$channelSessionID');
    await pusher.connect();
  } catch (e) {
    errorOccurred(mssg: e.toString());
    log("ERROR: $e");
  }
}

triggerPusher(
    {required String channelSessionID,
    required String mssg,
    required String userId}) async {
  final c = '$_c$channelSessionID';
  final _ = await pusher.trigger(PusherEvent(
    channelName: c,
    eventName:
        "client-message.sent", //You must use the "client-" prefix for an Event Name
    data: {'message': mssg},
    //userId: userId,
  ));
}

dynamic onAuthorizer(String channelName, String socketId, dynamic options) {
  return {
    "auth": "${PusherConfig.key}:${getSignature("$socketId:$channelName")}",
  };
}

getSignature(String value) {
  //Dont know how and why this worked. But I know its is needed for authorization when connecting to a private channel
  var key = utf8.encode(PusherConfig.secret);
  var bytes = utf8.encode(value);
  var hmacSha256 = Hmac(sha256, key); // HMAC-SHA256
  var digest = hmacSha256.convert(bytes);
  return digest;
}

disconnectPusher(String channelSessionID) async {
  await pusher.unsubscribe(channelName: '$_c$channelSessionID');
  await pusher.disconnect();
}

void onEvent(PusherEvent e) {
  if (e.data == {}) {
    return;
  }
  final event = e;
 //Update UI
}

const _c =
    'private-chat.'; //You must use the "private-" prefix for an channel Name if the channel is private else "public-" for public channels

void onSubscriptionSucceeded(String channelName, dynamic data) {
  log("onSubscriptionSucceeded: $channelName data: $data");
  final me = pusher.getChannel(channelName)?.me;
  log("Me: $me");
}

void onSubscriptionError(String message, dynamic e) {
  log("onSubscriptionError: $message Exception: $e");
}

void onDecryptionFailure(String event, String reason) {
  log("onDecryptionFailure: $event reason: $reason");
}

void onMemberAdded(String channelName, PusherMember member) {
  log("onMemberAdded: $channelName user: $member");
}

void onMemberRemoved(String channelName, PusherMember member) {
  log("onMemberRemoved: $channelName user: $member");
}

void onSubscriptionCount(String channelName, int subscriptionCount) {
  log("onSubscriptionCount: $channelName subscriptionCount: $subscriptionCount");
}

void onConnectionStateChange(dynamic currentState, dynamic previousState) {
  log("Connection: $currentState");
}

void onError(String message, int? code, dynamic e) {
  log("onError: $message code: $code exception: $e");
}

void log(String text) {
  if (kDebugMode) {
    print("LOG: $text");
  }
}

There seems to be a couple issues, one around the receipt of events (using onEvent) and another around triggering of events (pusher.trigger). What stands out to me is that both errors seem to include null values.

Are you able to log out parameters to ensure they are as expected? Could you also share any errors/code going forward as text? I can't copy/paste for replication, and it is difficult to consume code via screenshots.

If the parameter are not as expected, then it shouldnt have worked on web because the same code works on web but not on mobile.

Hello @benw-pusher I have been expecting a reply

Are you able to log out the parameters? I know you have mentioned that this works well on web, however the fact remains there is an error being thrown around null values and so this needs to be the next step in the investigation

Incase any one ever faced this issue.
Pls note the following.
The events returned by both platforms are different, on web events is of type Map, but on mobile it is of type String. So you you have to treat them differently. View the attached image for more details

Screenshot 2024-03-14 at 02 17 11