In flutter i'm using adhara_socket_io to open a socket and receive data from my server to push a notification using flutter_local_notifications and it works when the app is running, now i need to make this run in the background so when my app is not running it still receives data from my server and pushes notifications, is there anyway for doing that in flutter ?
void initSocket() async {
final deviceInfo = await Constants.getDeviceDetails();
final String deviceId = deviceInfo[2];
Uuid uuid = Uuid();
print(deviceInfo);
final id = uuid.v5(Uuid.NAMESPACE_URL, deviceId);
print('id ' + id);
SocketIOManager manager = SocketIOManager();
SocketIO socket = await manager.createInstance(
SocketOptions(URI,
enableLogging: false,
transports: [Transports.WEB_SOCKET /*, Transports.POLLING*/]),
);
socket.onConnect((data) {
print("connected...");
print(data);
socket.emit('settings', [
{'device_id': id}
]);
});
socket.onConnectError((_) => print('connect error'));
socket.onConnectTimeout((_) => print('timeout'));
socket.onError((_) => print('error'));
socket.onDisconnect((_) => print('disconnect'));
socket.on('notif', (data) {
print('notif');
_showNotificationWithDefaultSound(data['heading'],data['content']);
});
socket.connect();
}
initNotifications() {
var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(initializationSettingsAndroid,initializationSettingsIOS);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification: onSelectNotification);
}
Future onSelectNotification(String payload)async{
print('payload');
}
Future _showNotificationWithDefaultSound(heading,content) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
heading,
content,
platformChannelSpecifics,
payload: 'Default_Sound',
);
}
https://medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6e
hope this will help or u can use fcm https://fireship.io/lessons/flutter-push-notifications-fcm-guide/
however i am begginer .i dont know about this too much ..sorry if this doesnt help you.