0

I made a send request and accept/reject Notification functionality in my Application but in debug mode all notifications work properly but in apk release mode notifications are not working properly, I am stuck and can't get to the problem

shrinkResources false minifyEnabled false I had added these two lines in app/build.gradle but notifications still not working. Basically, my database collection is created when firebaseInit() function is called in NotificationServices class and this function runs only if a notification is sent. My notifications are not working so my database collection is not getting created and the data is not showing on the screen.

Here is the function that is called first after sending the notification.

void firebaseInit(BuildContext context) async {
  SharedPreferences sp = await SharedPreferences.getInstance();
  String userId = sp.getString('useId');
  FirebaseMessaging.onMessage.listen((message) async {

    RemoteNotification notification = message.notification;
    AndroidNotification android = message.notification.android;
    if (kDebugMode) {
      print("notifications title:${notification.title}");
      print("notifications body:${notification.body}");
      print('data:${message.data.toString()}');
    }
    String senderId = message.data['senderId'].toString();
    String senderName = message.data['senderName'].toString();
    String senderFcmToken = message.data['senderFcmToken'].toString();
    String status = message.data['status'].toString();
    String driverId = message.data['receiverId'].toString();
    String receiverName = message.data['receiverName'].toString();
    String rideId = message.data['rideId'].toString();
    String pickUpPlace = message.data['pickUpPlace'].toString();
    String destination = message.data['destination'].toString();
    String date = message.data['date'].toString();
    String profileUrl = message.data['profileUrl'].toString();


    if(status == "pending"){   FirebaseFirestore.instance.collection("rideRequests").doc(driverId).collection(rideId).doc(senderId).set({
        'receiverId': driverId,
        'receiverName': receiverName,
        'senderName': senderName,
        'senderFcmToken': senderFcmToken,
        'senderId': senderId,
        'rideId': rideId,
        'status': status,
        'date': date,
        'pickUpPlace': pickUpPlace,
        'destination': destination,
        'profileUrl': profileUrl
      }).whenComplete(() => print("*******Completed******"));
    }

    if(status == 'confirmed'){
      FirebaseFirestore db = FirebaseFirestore.instance;
      await  db.collection("rideRequests").doc(driverId).collection(rideId).doc(senderId).update(
          {
            "status": status
          }).whenComplete(() => print("Updated =====> $status")).onError((error, stackTrace) {
            print("Update function error ==========> " + error.toString());
      });
    }


    if(status == 'cancelled'){
      FirebaseFirestore db = FirebaseFirestore.instance;
      await  db.collection("rideRequests").doc(driverId).collection(rideId).doc(senderId).update(
          {
            "status": status
          }).whenComplete(() => print("Updated =====> $status")).onError((error, stackTrace) {
        print("Update function error ==========> " + error.toString());
      });
    }

    if (Platform.isIOS) {
      forgroundMessage();
    }

    if (Platform.isAndroid) {
      initLocalNotifications(context, message);
      showNotification(message);
    }
  });
}

Here is the Push Notification Code

           Center(
            child: (pending == false || pending == null) ? ElevatedButton(
              onPressed: () async {
                print(widget.fcmToken);
                print(widget.driverName);
                SharedPreferences sp = await SharedPreferences.getInstance();
                String profileURL = sp.getString('profilepic');


                try {
                  print("====================> Notification Send");

                  final body = {
                    "to": widget.fcmToken,
                    "notification": {
                      "title": "$userName send a ride request",
                      "body": "Are you ready for this ride?",
                      "android_channel_id": "Vision DG Tech"
                    },
                    'data': {
                      'type': 'notify',
                      'status': 'pending',
                      'senderFcmToken': widget.senderToken,
                      'senderName': userName,
                      'senderId': userId,
                      'receiverId': widget.driverId,
                      'receiverName': widget.driverName,
                      'rideId': widget.rideId,
                      'profileUrl': profileURL,
                      'pickUpPlace': widget.pickUpPlace,
                      'destination': widget.destination,
                      'date': widget.date.toString()
                    }
                  };
                  // print('$userId \t $userName');

                  var res = await post(
                    Uri.parse('https://fcm.googleapis.com/fcm/send'),
                    body: jsonEncode(body),
                    headers: {
                      HttpHeaders.contentTypeHeader:
                          'application/json; charset=UTF-8',
                      HttpHeaders.authorizationHeader: "key=$key"
                    },
                  );
                  pending = true;
                  setState(() {});
                  // Get.to(MyBottomBar());
                  if (kDebugMode) {
                    print('Res -------- ${res.body}');
                  }
                } catch (_) {
                  if (kDebugMode) {
                    print("Catch body called in send request notification");
                  }
                }
                setState(() {});
                
                
              },
              child: Text("Send Request"),
              style: ButtonStyle(
                  backgroundColor:
                      MaterialStatePropertyAll(pSecondaryColor)),
            ) 
                : Center(child: Column(
                  children: [
                    Text("Your Request has been sent", textAlign: TextAlign.center,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.arrow_back_ios, color: Color(0xFF1349eb),),
                        TextButton(onPressed: (){
                          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MyBottomBar()));
                        }, child: Text("Go to Main Page", style: TextStyle(color: Color(0xFF1349eb), fontSize: 16),)),
                      ],
                    )
                  ],
                ))
          )

0 Answers0