skip to Main Content

I have made a SOS application using Laravel.
Everything is working properly on the backend side.
And I have implemented the client side with Flutter
Now I want to send a message to the target audience with the API when the user clicks on the SOS button in the application.
I have done this, but I can’t do it in real time, I refresh the application with a timer.
How can I run it in real time?

  • I have used the pusher service
  • I don’t want to use firebase

thank you

2

Answers


    • Try StreamBuilder
      this work for me
    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    class HomePage extends StatelessWidget {
    
    Stream<http.Response> getRandomNumberFact() async* {
    
    yield* Stream.periodic(Duration(seconds: 5), (_) {
    
       return http.get("http://numbersapi.com/random/");
    
       }).asyncMap((event) async => await event);
     }
    
    @override
    
    Widget build(BuildContext context) {
      return StreamBuilder<http.Response>(
             stream: getRandomNumberFact(),
             builder: (context, snapshot) => snapshot.hasData
              ? Center(child: Text(snapshot.data.body))
              : CircularProgressIndicator(),
           );
      }
     }
    
    Login or Signup to reply.
  1. Yo, so many way to implement this feature, you can use:

    1. Websocket or SocketIO (difficult: HIGH).
    2. Push notification (difficult: MEDIUM).

    1. Use websocket or socketio:

    Laravel will implement socket feature (you can find the way to implement socket.io for laravel on Google).
    Then, flutter app will connect to socket and receive the event to trigger actions, refer this library will help you: https://pub.dev/packages/socket_io_client

    I think, if you fresh new with flutter, you can use push notification instead, because with websocket/socket.io, you need spend more time to handle connection handshake, process/convert data and many connection issue relates (SSL, testing no SSL, backend porting, protocol,…)

    2. Use push notification:

    Many services will help you like Firebase messaging, Onesignal push notifications,… but if you need to implement for Android, you should use Firebase messaging and android device should has PlayService (play store installed).

    Backend:

    • In Laravel backend, you should implement firebase admin (refer: https://github.com/kreait/laravel-firebase).
    • The laravel backend will store user tokens, tokens will help firebase know which user should be receive notifications.
    • When need to trigger mobile client, laravel will use firebase admin messaging module to send.

    Flutter:

    2.1. Solution on Laravel:

    1. Update your login code: when user login, your login endpoint will add a field to receive FCM token from mobile, you will store this token for send notification later.
    2. Add new api endpoint to update FCM token: the FCM maybe change after 3 months or when user reset app,… so you need to write api to receive new token from client side when needed.

    2.2. Solution on Flutter:

    • You will write a class to receive or handle FCM token changes, this class will check token on client side and send to laravel backend (2.1.2). I refer you save token into local storage (eg. use shared_preferences library on pub.dev to store), then get token and send to backend later when needed.
    • Implement notification receiver, check documentation at: https://firebase.flutter.dev/docs/messaging/usage#foreground-messages .

    Note: The push notification will also send notification to user, if you no need to display notification, just implement trigger only, use data notification.

    I will let you know the summary of implementation, the detail is too long and maybe not your choice, so if you need more detail, please comment, I will update.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search