pusher / push-notifications-flutter

Official Pusher Beams Flutter client plugin (iOS, Android and Web), receive notifications easily on your Flutter application with Pusher Beams.
https://pub.dev/packages/pusher_beams
MIT License
10 stars 27 forks source link

The flutter plugin is not able to set the user id. #24

Open mmc-eastwolf opened 2 years ago

mmc-eastwolf commented 2 years ago

Laravel route that returns the token :

public function beamsAuth(PusherAuthRequest $request)
    {
        try {
            $beamsClient = new PushNotifications([
                'instanceId' => env('PUSHER_BEAMS_INSTANCE_ID'),
                'secretKey' => env('PUSHER_BEAMS_KEY'),
            ]);
            $user = $request->user(); // If you use a different auth system, do your checks here
            $userID = $user['id'];
            $beamsToken = $beamsClient->generateToken("".$userID);
            $action = new CreateDeviceAction();
            $dto = DeviceDataTransferObject::fromArray([
                'os' => $request->input('os') ?? null,
                'timezone' => $request->input('timezone') ?? null,
                'token' => $beamsToken['token'] ?? null,
                'alfred_id' => $user['id'] ?? null,
            ]);
            $action($dto);
            return response()->json($beamsToken);
        } catch (\Exception $e) {
            throw new HttpResponseException(response()->json([
                'status' => false,
                'error' => $e->getMessage()
            ], 400));
        }
    }

Response from our server :

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciO............."
}

Error :

When the exception was thrown, this was the stack:
#0      List.[] (dart:core-patch/array.dart:121:36)
#1      PusherBeams.handleCallback (package:pusher_beams/pusher_beams.dart:297:22)
#2      CallbackHandlerApi.setup.<anonymous closure> (package:pusher_beams_platform_interface/method_channel_pusher_beams.dart:404:15)
#3      CallbackHandlerApi.setup.<anonymous closure> (package:pusher_beams_platform_interface/method_channel_pusher_beams.dart:391:35)
#4      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:77:49)
#5      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:76:47)
#6      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:377:35)
#7      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:374:46)
#8      _invoke2.<anonymous closure> (dart:ui/hooks.dart:190:15)
#12     _invoke2 (dart:ui/hooks.dart:189:10)
#13     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:42:5)
#14     _Channel.push (dart:ui/channel_buffers.dart:132:31)
#15     ChannelBuffers.push (dart:ui/channel_buffers.dart:329:17)
#16     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:583:22)
#17     _dispatchPlatformMessage (dart:ui/hooks.dart:89:31)
(elided 3 frames from dart:async)

Flutter doctor output :

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.5, on macOS 12.4 21F79 darwin-arm, locale en-RO)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.67.2)
[✓] Connected device (2 available)
    ! Error: Marius's iPhone is not connected. Xcode will continue when Marius's iPhone is connected. (code -13)
[✓] HTTP Host Availability

• No issues found!
oppakun commented 2 years ago

if you got RangeError you need to catch it ` @override void handleCallback(String callbackId, String callbackName, List args) { final callback = _callbacks[callbackId]!;

switch (callbackName) {
  case "onInterestChanges":
    callback((args[0] as List<Object?>).cast<String>());
    return;
  case "setUserId":
    try{
      callback(args[0] as String?);
    }on RangeError{
      callback("sucess");
    }

    return;
  case "onMessageReceivedInTheForeground":
    callback((args[0] as Map<Object?, Object?>));
    return;
  default:
    callback();
    return;
}

}` because when success args is empty list

benjamin-tang-pusher commented 2 years ago

PusherBeams.instance.setUserId(...) works fine for me. Can you paste your client code here as well? The server's response looks correct.

mmc-eastwolf commented 2 years ago

This is the function that should create the token.

getSecure() async {
    final BeamsAuthProvider provider = BeamsAuthProvider()
      ..authUrl = 'https://<my-server>/secure'
      ..headers = {'Content-Type': 'application/json'}
      ..credentials = 'omit';

    await PusherBeams.instance.setUserId(
        'user-id',
        provider,
        (error) => {
              if (error != null) {print(error)}

              // Success! Do something...
            });
  }
mmc-eastwolf commented 2 years ago

Any updates on this ?