Flutter Signalr Listener is not connected in 2nd screen after migrated to null safety stable version - flutter

Chatting was working perfectly before migrating to null safety using signalr. But after migrating It is not working in chatting part.
Scenario is like there are 2 screens where I am using signalr.
1)Chatlist.
2)Chatting with person.
listener in Chatlist is perfect but in 2nd screen it is not working(Just worked when I installed and run for the 1st time). Weird issue.
All was working in old. I am using bloc for statemanagement and also migrated to yield to emit.
Piece of code is like:
void listenOnMessageReceived(
HubConnection hubConnection,
Function(Message? chatMessageReceive) onMessageReceived,
) {
final SocketResponseCallBack chatMessageReceived =
(response) => onMessageReceived(Message.fromJson(response));
final hubMethod = HubMethod(
CHAT_RECEIVED_MESSAGE_METHOD_NAME,
SignalRHelper.toSocketFunction(
CHAT_RECEIVED_MESSAGE_METHOD_NAME, chatMessageReceived));
bool exists = listenOnHubMethod.any((method) => method.methodName == CHAT_RECEIVED_MESSAGE_METHOD_NAME);
if(exists) {
listenOnHubMethod.removeWhere((element) =>
element.methodName == CHAT_RECEIVED_MESSAGE_METHOD_NAME);
SignalRHelper(hubConnection: hubConnection).on(
hubMethod.methodName,
hubMethod.methodFunction,
);
listenOnHubMethod.add(hubMethod);
}else{
SignalRHelper(hubConnection: hubConnection).on(
hubMethod.methodName,
hubMethod.methodFunction,
);
listenOnHubMethod.add(hubMethod);
}
}
I am having 2 types of above code in different screens. but it is working in only 1 screen and not listening in 2nd screen.
here is a piece of signalr listener code:
static MethodInvocationFunc toSocketFunction(
String methodName, SocketResponseCallBack responseCallBack) {
return (arguments) {
try {
if (arguments!.isEmpty) {
throw SocketEmptyResponseException(methodName);
}
final response = arguments.first;
responseCallBack(response);
} on FormatException {
throw SocketResponseException(methodName);
}
};
}
Is there any limitations in migration of stable version or anything else. Every help is appreciable.
Thank you.

Do not use signalR, on IOS it will be impossible to run listener on background or when app is closed and you will miss messages. Use FCM.

Related

Flutter Push Notification using SignalR

I'm using SignalR for push notifications on my Flutter app and that works ok. I get the message from the backend and show notification using flutter_local_notifications. The problem is that the SignalR service would shut down after some time.
How can I make my app stay on in the background? and even start on reboot?
Here's my code:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:isolate_test/Model/UserMessageModel.dart';
import 'package:signalr_core/signalr_core.dart';
import 'EndPointService.dart';
import 'NotificationService.dart';
class SignalRProvider {
static String appName = "NOTIFICATION";
static String? userName = "";
static String deviceName = "android_app";
static List<UserMessageModel> messages = <UserMessageModel>[];
HubConnection connection = HubConnectionBuilder()
.withUrl(
'my_url',
HttpConnectionOptions(
logging: (level, message) => print(message),
))
.withAutomaticReconnect()
.withHubProtocol(JsonHubProtocol())
.build();
Function(bool update)? onMessagesUpdateCallback;
SignalRProvider({
this.onMessagesUpdateCallback,
});
setUsername(String username) {
userName = username;
}
Future initSignalR(BuildContext context) async {
WidgetsFlutterBinding.ensureInitialized();
await NotificationService().init();
connection.on('SignalRUserReceiveMessage', (message) async {
var data = message!.first;
if (data != null) {
UserMessageModel msg = UserMessageModel.fromJson(data);
messages.add(msg);
msg.showNotification();
}
if (onMessagesUpdateCallback != null) {
onMessagesUpdateCallback!(true);
}
});
connection.on('SignalRMonitoringMessage', (message) async {
var data = message!.first;
if (data != null) {
UserMessageModel msg = UserMessageModel.fromJson(data);
messages.add(msg);
msg.showNotification();
}
if (onMessagesUpdateCallback != null) {
onMessagesUpdateCallback!(true);
}
});
connection.on("SignalRReceiveConnectedMessage", (message) async {
await connection.send(methodName: 'SignalRInit', args: [
userName,
appName,
connection.connectionId,
]);
});
connection.on("SignalRReceiveDisconnectedMessage", (message) async {
if (connection.state == HubConnectionState.disconnected) {
connection.start();
}
});
await connection.start();
}
List<UserMessageModel> getMessages() {
return messages;
}
Future deleteMessage(UserMessageModel _msg) async {
if (_msg == null) return;
var response =
await EndPointService().SetupApi("Message", "", []).httpDelete(
HeaderEnum.BasicHeaderEnum,
ResponseEnum.ResponseModelEnum,
jsonEncode(_msg),
);
}
addOrUpdateMessage(UserMessageModel _msg) {
if (_msg == null) return;
if (messages != null) {
var found =
messages.firstWhere((e) => e.user == _msg.user && e.id == _msg.id);
var index =
messages.indexWhere((e) => e.user == _msg.user && e.id == _msg.id);
if (found != null) {
messages[index] = _msg;
} else {
messages.add(_msg);
}
if (onMessagesUpdateCallback != null) {
onMessagesUpdateCallback!(true);
}
}
}
setMessagesUpdateCallback(Function(bool update) func) {
onMessagesUpdateCallback = func;
}
}
SignalR problems
SignalR for Flutter uses web sockets and SSE to receive messages from the SignalR service. If the app was terminated because the user restarted their phone or the OS shut down the app to save battery, these push notifications would not be received by the app.
To overcome this, app developers (and SignalR) have to use FCM on Android, and APNs on iOS (or FCM which will also use APNs on iOS). All other approaches will be more limited because the operating systems do not allow users to keep background processes running the entire time. This was actually allowed years ago, but the operating systems have made these changes to save the user battery - they enforce that all apps go through the same push notification medium - FCM on Android, APNs on iOS.
SignalR for Flutter uses neither FCM nor APNs. At it's current state, SignalR is not well suited for Android or iOS - take a look at the comments with people struggling with similar problems to you on How to use signalr in Android.
Alternative solution
The simplest / easiest way to get started is to use Firebase Cloud Messaging.
On Android, it will be used directly to send messages to devices, and
on iOS, FCM will use APNs to reach devices reliably
Caveat: On Android, there is a more complicated alternative called unifiedpush, but the limitations include showing a notification to the user at all times to handle background notifications.
My analysis: This is all done based on my quick investigation by reading the pubspec.yaml, the GitHub issues on the original repo, the SignalR documentation, and some experience implementing Push Notifications for Flutter.
Disclosure: I just released a push notification library 2 days ago called push which would be well suited to these types of Push Notification packages making the transformation to using FCM on Android and APNs on iOS. However, as an app developer, in most cases, you should use firebase_messaging, not push.
I worked with SignalR but on native Platform(IOS & Android), I made stock app and get realtime price. When app go to background, I will disconnect with SignalR server after 5 second, and when app go to foreground again, I check if app's current state not connect to server SignalR, I'll connect again. I think it not good if your app still connect and receiver data from signalR server in background state.

flutter embedded pos printer (Like: Android Q2 device)

Already I have built a flutter project. Now I need to print from a pos embedded device. I am googling, but I don't get any solution.
Please help me if there is any solution.
Actually I need for Android Q2 device
 
I have the same device and i already built flutter application , and ran into the same probleme .
I contacted the company and they provided me with android sdk so i added a channel and called the print from the flutter code.
EDIT:
In your case, you can download the SDK provided by the company and write native code to print the receipt.
example:
in flutter
static const platform = const MethodChannel('com.example.myapplication');
Future<void> _print() async {
try {
final bool result = await platform.invokeMethod('print',{"data":Printdata});
if (result) {
// success
} else {
// failed
}
} on PlatformException catch (e) {
//failed to print
}
}
Then in the Main.java / Main.kt implement the method from the SDK documentation
example:
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("print")) {
data = call.argument("data");
// Code from SDK documentation
} else {
result.notImplemented();
}
}
ref: Example Nativcode in flutter
Add third party SDK to android
Try this library "flutter_bluetooth_serial" and connect to the printer via direct mac address like this:
BluetoothConnection connection = await BluetoothConnection.toAddress("mac");

Detecting an incoming call in my Unity app

I am trying to make my game pause on incoming phone call. I am wondering if any of this functions which I used can do this. I have used them in my source code but none of them worked.
void OnApplicationPause(bool paused)
{
if (paused == true)
{
if (!isPaused)
PauseResume(true);
}
}
void OnApplicationFocus(bool hasFocus)
{
if (hasFocus == false)
{
if (!isPaused)
PauseResume(true);
}
}
Also i had found Application.runInBackground() but it is mentioned in documentation that "Note: This property is ignored on Android and iOS".
In iOS and Android, OnApplicationPause(...) is expected to be called. This user had the same issue: https://forum.unity.com/threads/onapplicationpause-not-being-called-in-ios-build.455426/
His answer was:
Apparently, it was not working because I had 'Behaviour in Background' set to Custom instead of Suspend...

Getting some chinese characters when trying to access google play username with Unity3D

I'm getting some chinese characters when trying to access google play username with Unity3D:
void Start () {
PlayGamesPlatform.Activate();
// Social.localUser.Authenticate(ProcessAuthentication);
PlayGamesPlatform.DebugLogEnabled = true;
Social.localUser.Authenticate(success => {
if (success)
{
Debug.Log("Authentication successful");
userInfo = Social.localUser.userName ;
Debug.Log(userInfo);
}
else
Debug.Log("Authentication failed");
});
}
void Update () {
txt = GameObject.Find("txt").GetComponent<Text>();
txt.text = userInfo;
}
}
I have checked if user is really authenticated with google play, and he is. I'm getting this error on my mobile phone (Samsung S6).
Any ideas how to solve this?
I had the same problem, solved by updating google play games plugin
I was having the same problem as well with the 0.9.38 version of GPGS. They committed a fix 2 days ago (v0.9.38a) that appears to have fixed the issue. From the commit log:
Fixing string marshaling from C to C#.
Make sure you follow the upgrade instructions when upgrading.

Akavache not working in Windows 8.1 Universal App

I’m trying to make work Akavache in a Windows Universal Application (8.1 for now, using ReactiveUI 6.5).
To make sure that it is not related to my architecture, I did an empty solution that has all the necessary packages and requirements (VC++ for both platforms), and I still get the same issue. This is a blocker for me since I want all my queries to be cached.
Here's the code:
BlobCache.ApplicationName = "MyApp"; // In AppBootstrapper`
// In My ViewModel
SubmitCommand = ReactiveCommand.CreateAsyncTask(async _ =>
{
var isTrue = await BlobCache.UserAccount.GetOrFetchObject("login_credentials",
async () => await Task.FromResult(true)
);
// My Code never goes further than this
if (!isTrue)
{
throw new Exception("I'm false!");
}
return isTrue;
});
SubmitCommand.Subscribe(isTrue => {
Debug.WriteLine("I got a new value!");
});
SubmitCommand.ThrownExceptions.Subscribe(ex => {
UserError.Throw(ex.Message, ex);
});
// In The View
ViewModel = new MainPageViewModel();
this.BindCommand(ViewModel, x => x.SubmitCommand, x => x.SubmitCommand);
public MainPageViewModel ViewModel
{
get { return (MainPageViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(MainPageViewModel), typeof(MainPage), new PropertyMetadata(null));
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (MainPageViewModel)value; }
}
Edit After some debug, Windows Phone 8.1 Silverlight works, not Jupiter.
So what's missing?
I'm using RXUI 6.5 (latest) with a Windows Phone 8.1 (Jupiter) (with shared Universal Projects)
Updated: Akavache.Sqlite3 is causing the issue. InMemoryCache is working (removing Akavache.Sqlite3 "fixes" the problem), but not Sqlite3.
Also, registering BlobCache's different types of cache (copy paste from https://github.com/akavache/Akavache/blob/3c1431250ae94d25cf7ac9637528f4445b131317/Akavache.Sqlite3/Registrations.cs#L32) is working apparently.. so I suppose the Registration class aren't working properly and calling
new Akavache.Sqlite3.Registrations().Register(Locator.CurrentMutable); is not working.
Edit: My temporary solution is to copy paste this into my application, and I invoke it after BlobCache.ApplicationName. It works, but I shouldn't technically have to do that.
Thanks for your help