Kavinda Lochana
Kavinda Lochana

Reputation: 125

How do we get flutter iOS data notification when the app is in terminated state

I managed to make foreground and background notifications. But I can't get notifications when the app is in terminated screen. But only on iOS. Android is working fine.

Here is the process briefly. I am using sending server side notifications. Android working fine at the moment. iOS not working. I use these notifications to get a video call screen(or ios call screen). My problem is I can't get data notifications to ios when it's in terminated state. But normal push notifications are coming.

This is the code I use for server side

 $fields = array(

                'registration_ids' => array (
                    $login_device_token->token,
            ),
            "content_available"=> true,
            "priority"=>"high",

                "notification" => array(
                    "sound"=> 'default',
                    
                ),
                    "data" => array(
                       
                        "type" => $msg_type,
                        "id" => $msg_id,
                        "sub_no" => $sub_no,
                        "name" => $name,
                        "click_action" =>'FLUTTER_NOTIFICATION_CLICK'
                    ),
                   
            );

Here is the onBackground function

 FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
  var initialMessage = await FirebaseMessaging.instance
      .getInitialMessage()
      .then((value) async {
    await myBackgroundMessageHandler(message.data);
    await initConnect();
  });
  print(' onMessageOpenedApp  ${message.data}');
  print(' onMessageOpenedApp Notification data:::: ');
  if (!_isCameCall) {

    _isCameCall = true;
    // ---------- Get Call Notification -------------------
    if (message.data['type'] == 'call') {
      debugPrint(
          '----------- Sending trought onBackgroundMessage----------');
      await myBackgroundMessageHandler(message.data);
      await initConnect();
    }
  }
});




Future<void> _firebaseMessagingBackgroundHandler(
        RemoteMessage message) async {
      var initialMessage = await FirebaseMessaging.instance
          .getInitialMessage()
          .then((value) async {
        await myBackgroundMessageHandler(message.data);
        await initConnect();
      });
      await Firebase.initializeApp();
     ;
      if (!_isCameCall) {
        _isCameCall = true;
        // ---------- Get Call Notification -------------------
        if (message.data['type'] == 'call') {
          debugPrint('call type == call ------------');
          await myBackgroundMessageHandler(message.data);

// ---------- ANSWER AND REJECT CALL FUNCTIONS -----------------
          await initConnect();
        }
      }
    }

Me and my friend implementing this app. So do you guys have any idea to slove this issue?

I am using flutter_local_notification package and connectycube_flutter_call_kit .

I searched for a solution but nothing found. Any solution for this matter very much appreciated.

Upvotes: 3

Views: 1048

Answers (1)

Ahmed Elhenidy
Ahmed Elhenidy

Reputation: 342

if you intend to use firebase services when receiving then you should call await Firebase.initializeApp(); before using the FirebaseMessaging.instance.getInitialMessage();

void handleBackgroundNotifications() {
 FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}

call handleBackgroundNotifications in the main like this

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //* Initialize Firebase
  await Firebase.initializeApp();
  handleBackgroundNotifications();
}

here is some edits on your function

Future<void> _firebaseMessagingBackgroundHandler(
    RemoteMessage message) async {
// make sure you initialize your app first
  await Firebase.initializeApp();
  var initialMessage = await FirebaseMessaging.instance
      .getInitialMessage()
      .then((value) async {
    await myBackgroundMessageHandler(message.data);
    await initConnect();
  });
  if (!_isCameCall) {
    _isCameCall = true;
    // ---------- Get Call Notification -------------------
    if (message.data['type'] == 'call') {
      debugPrint('call type == call ------------');
      await myBackgroundMessageHandler(message.data);

 // ---------- ANSWER AND REJECT CALL FUNCTIONS -----------------
      await initConnect();
    }
  }
}

Upvotes: 2

Related Questions