Home > database >  Flutter Dio helper post return null value
Flutter Dio helper post return null value

Time:01-11

I used Fire Base messaging to send message for another device . i got the key and token of another device but the post just working in postman and i receive notification successfully on my device . but when i use code like below it returns null value Why is the value return from post request in send message Function is null . the post request is working fine in postman and i didn't see any logic error hope someone help me in this problem


import 'package:dio/dio.dart';
class DioHelper{

  static Dio ?dio ;


  static init(){
    
    dio = Dio(
      BaseOptions(
        baseUrl: 'https://fcm.googleapis.com/fcm/',
        receiveDataWhenStatusError: true,

      ) ,
    ) ;

  }

  static Future<Response?> getData({
    required String url,
    Map<String, dynamic> ?query,
    String lang = 'en',
    String ?token,
  }) async
  {
    dio?.options.headers =
    {
      'Content-Type':'application/json',
      'Authorization': 'key=key=${myapi}',
    };

    return await dio?.get(
      url,
      queryParameters: query??null,
    );
  }

  static Future<Response?> postData({
    required String url,
    Map<String, dynamic> ?query,
    required Map<String,dynamic> data ,
  })async
  {
    dio?.options.headers={
      'Content-Type':'application/json',
      'Authorization': 'key=${myapi}',
    };
    return await dio?.post(url,data: data ,queryParameters: query) ;
  }

  static Future<Response?> putData({
    required String url,
    Map<String, dynamic> ?query,
    required Map<String,dynamic> data ,
    String lang='en' ,
    String ?token ,
  })async
  {
    dio?.options.headers={
      'Content-Type':'application/json',
      'Authorization': 'key=key=${myapi}',
    };
    return await dio?.put(url,data: data ,queryParameters: query) ;
  }

}

the function that use Di-helper

void sendMessageForOneUser(String tokens,String title,String body,String image){
    print('sendmessages');
    DioHelper.postData(url:'send',data:{
      "to":tokens,
      "notification":{
        "title": title,
        "body":body ,
        "mutable_content": true,
        "sound": "Tri-tone",
        "image":image
      }
    }).then((value){
      print(value);
    }).catchError((onError){
      print(onError.toString());
    });
  }

i don't know why not working thought it worked on postman fine

CodePudding user response:

Using "key=your_server_key" in client-side code is a serious security risk, as it allows malicious users to send whatever message they want to your users. This is a bad practice and should not be used in production-level applications.

You can try this code to send push notification from client side (App side) but i suggest you to avoid this way until and unless your not using server for your mobile application. try to call own server api to send push notification from your server side instead send push notification from client (mobile app side).

Future<void> sendPushNotification(String receiverToken) async {
  try {
    const postUrl = 'https://fcm.googleapis.com/fcm/send';
    final data = {
      "registration_ids": [receiverToken], //CAN pass multiple tokens
      "collapse_key": "type_a",
      "notification": {
        "title": 'NewTextTitle',
        "body": 'NewTextBody',
      }
    };

    final headers = {
      'content-type': 'application/json',
      'Authorization': 'FCM_API_SERVER_KEY' // 'key=YOUR_SERVER_KEY'
    };

    final response = await http.post(postUrl,
        body: json.encode(data),
        encoding: Encoding.getByName('utf-8'),
        headers: headers);

    if (response.statusCode == 200) {
      debugPrint('test ok push FM');
    } else {
      debugPrint(' FCM not sent successfully');
    }
  } catch (ex) {
    debugPrint('send push notification api error: $ex');
  }
}

for better approach checkout this link

  • Related