pusher / pusher-channels-flutter

Pusher Channels client library for Flutter targeting IOS, Android, and WEB
MIT License
74 stars 133 forks source link

Pusher not updating on Android #144

Closed Princewil closed 8 months ago

Princewil commented 10 months ago

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

Screenshot (100) Screenshot (101)

benw-pusher commented 10 months ago

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

Princewil commented 10 months ago

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

These are the screenshots.

Princewil commented 9 months ago

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

Screenshot (112)

Princewil commented 9 months ago

Screenshot (113)

Another image

benw-pusher commented 9 months ago

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.

Princewil commented 9 months ago

Alright. I will send that in a few of hours from now.

On Fri, 2 Feb 2024 at 16:47, benw-pusher @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/pusher/pusher-channels-flutter/issues/144#issuecomment-1924142680, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJWWTE5TFSQE6TYWSBWT7OLYRUDCRAVCNFSM6AAAAABCPGMWJCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRUGE2DENRYGA . You are receiving this because you authored the thread.Message ID: @.***>

Princewil commented 9 months ago
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");
  }
}
Princewil commented 9 months ago

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.

Princewil commented 9 months ago

Hello @benw-pusher I have been expecting a reply

benw-pusher commented 9 months ago

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

Princewil commented 9 months ago

I tired but I don’t seem to find a way to log the parameters. Do you have a way in mind?

On Wed, 28 Feb 2024 at 12:54, benw-pusher @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/pusher/pusher-channels-flutter/issues/144#issuecomment-1968823795, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJWWTE3UMSJVH5FB27CEPBDYV4LH5AVCNFSM6AAAAABCPGMWJCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRYHAZDGNZZGU . You are receiving this because you authored the thread.Message ID: @.***>

Princewil commented 8 months ago

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