gpt4 book ai didi

使用 SignalR 的 Flutter 推送通知

转载 作者:行者123 更新时间:2023-12-02 16:00:20 24 4
gpt4 key购买 nike

我在我的 Flutter 应用程序上使用 SignalR 推送通知,效果很好。我从后端获取消息并使用 flutter_local_notifications 显示通知。问题是 SignalR 服务会在一段时间后关闭。如何让我的应用程序在后台运行?甚至在重启时开始?

这是我的代码:

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 问题

SignalR for Flutter 使用网络套接字和 SSE 接收来自 SignalR service 的消息.如果应用因用户重启手机或操作系统关闭应用以节省电量而终止,则这些推送通知将不会被应用接收

为了克服这个问题,应用程序开发人员(和 SignalR)必须在 Android 上使用 FCM,在 iOS 上使用 APN(或者在 iOS 上也使用 APN 的 FCM)。所有其他方法将受到更多限制,因为操作系统不允许用户始终保持后台进程运行。这实际上在几年前是允许的,但操作系统已经做出这些更改以节省用户电量 - 它们强制所有应用程序通过相同的推送通知媒介 - Android 上的 FCM,iOS 上的 APN。

Flutter 的 SignalR 既不使用 FCM 也不使用 APNs。在当前状态下,SignalR 不太适合 Android 或 iOS - 请查看与您在 How to use signalr in Android 上遇到类似问题的人的评论。 .

替代方案

最简单/最简单的入门方法是使用 Firebase Cloud Messaging .

  • 在 Android 上,它将直接用于向设备发送消息,并且
  • 在 iOS 上,FCM 将使用 APN 可靠地访问设备

警告:在 Android 上,有一个更复杂的替代方案,称为 unifiedpush ,但限制包括始终向用户显示通知以处理后台通知。

我的分析:这一切都是基于我通过阅读 pubspec.yaml 的快速调查完成的。 , original repo 上的 GitHub 问题, SignalR documentation ,以及一些为 Flutter 实现推送通知的经验。

披露:我在 2 天前刚刚发布了一个名为 push 的推送通知库。这将非常适合这些类型的推送通知包,从而转变为在 Android 上使用 FCM 和在 iOS 上使用 APNs。但是,作为应用程序开发人员,在大多数情况下,您应该使用 firebase_messaging , 不是 push .

关于使用 SignalR 的 Flutter 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70829593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com