Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notification in Flutter when the app is turn off

I implemented push notification(cloud messaging ) however I could not get the notification when the app is totally turned off. So, I searched about that and i found that I need to use local notification. I started using local notifcation but I found that local notification is based on user's sides by schedualing events for example by the users themselves. So, the question is that how can I send a notification to all users using flutter_local_notifications?

Thank you

like image 237
Abdulsalam Fadhel Avatar asked Mar 29 '21 10:03

Abdulsalam Fadhel


People also ask

How do I trigger push notifications on Flutter?

In the main app target, select Signing & Capabilities > All > + Capability and then search "push." Double-click on Push Notifications to enable it. Next, enable Background Modes and check Remote Notifications. Now you are ready to send notifications from the OneSignal dashboard!

How do I turn off push notifications on Flutter?

If you want to disable push notifications for the user, use Notifications. setPushNotificationsEnabled(false) . When push notifications are disabled, you still can query for GetSocial Notifications via data API. To enable push notifications use Notifications.


Video Answer


2 Answers

This question has two-part actually.

  1. Not receiving a notification while the app is closed
  2. How can you send a notification to other devices by using the flutter_local_notifications package.

For the 'Not receiving' part:

If you've integrated FCM properly, you shouldn't have to worry about not getting push notifications at all while in the background or the the app is killed already.

FCM will automatically handle push notifications for you while you're in the background or the app is killed already.

Just make sure, you've integrated FCM properly to your flutter project.

Follow this link for proper integration. Also, iOS integration is a bit tricky and lengthy, follow this link for APNs integration. If you did not set up APNs properly with FCM, apple will not be able to recognize push notification trigger for background state.


Now, for the "Sending notification to other devices via flutter_local_notifications package:

you can't push notification via flutter_local_notifications which will delegate with FCM. You have to send an HTTP request via REST API call for that.

Sample code is given below:

Future<Response> publishNotification() async {
  return await post(
    'https://fcm.googleapis.com/fcm/send',
    headers: <String, String>{
      'Content-Type': 'application/json',
      'Authorization': 'key=$firebaseServerKey',
    },
    body: jsonEncode(
      <String, dynamic>{
        "to": firebaseDeviceToken,
        'priority': 'high',
        "data": <String, dynamic>{
          'click_action': 'FLUTTER_NOTIFICATION_CLICK',
          "body": "Order#10",
          "title": "New Order",
          "status": "done",
          "screen": "NotificationPage",
          "description": "Order Details",
        }
      },
    ),
  );
}

You might add that I want to send notifications to multiple devices. For that, just replace token with registration_ids and put your device token list on registration_ids.

Sample code:

Future<Response> publishNotification() async {
  return await post(
    'https://fcm.googleapis.com/fcm/send',
    headers: <String, String>{
      'Content-Type': 'application/json',
      'Authorization': 'key=$firebaseServerKey',
    },
    body: jsonEncode(
      <String, dynamic>{
        "registration_ids": ["token_1", "token_2", ...],
        'priority': 'high',
        "data": <String, dynamic>{
          'click_action': 'FLUTTER_NOTIFICATION_CLICK',
          "body": "Order#10",
          "title": "New Order",
          "status": "done",
          "screen": "NotificationPage",
          "description": "Order Details",
        }
      },
    ),
  );
}

I hope that I got you covered. Happy coding :D

like image 61
Omar Khaium Chowdhury Avatar answered Oct 21 '22 15:10

Omar Khaium Chowdhury


I can't imagine local notificaions would solve your problem.

According to the docs:

There are a few preconditions which must be met before the application can receive message payloads via FCM:

  • The application must have opened at least once (to allow for registration with FCM).
  • On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.
  • On Android, if the user force quits the app from device settings, it must be manually reopened again for messages to start working.

If these conditions are not met, your notification is simply never received. If no notification is received, then what are you going to display with local notifications?

What you could do is use local notificaions in combination with background fetch. With background fetch you can check some api whether there are some notifications. You'd have to somehow figure out which ones have been presented already and which ones not. Then you could use local notifications to present those. In that case I think it would be easier to drop cloud messaging and only use background fetch + local notifications.

On iOS you'd still face the same problem with background fetch. According to their docs:

When your app is terminated, iOS no longer fires events.

Background fetch

like image 2
Robin Dijkhof Avatar answered Oct 21 '22 16:10

Robin Dijkhof