A
A
atomnato2021-08-27 13:05:50
Android
atomnato, 2021-08-27 13:05:50

Push notifications when if app is closed?

When developing push notifications that work in the background (like notifications from instagram, telegram, vk), in my application, I had difficulties with their work in the background and closed mode. To write them, I used a plugin from FireBase ( Cloud Messaging ).
When sending a Notification message, notifications are received when the application is running in the background, in the case when the application is closed, notifications do not arrive at all. I tried to intercept responses from the server using the onResume and onLaunch listeners, but the first one worked only if the user was transferred to the application through a notification, the second one worked only the first time the application was launched.
When sending a Data message, the onResume and onLaunch listeners did not work at all.

In general, the main problem is that I don’t understand how to catch the response from the server about the arrival of a new message and process it when the application is running in the background and closed. I would be glad if you also provide links to at least some implementation.

Request body for notification Message:
6128bba0236fc034404092.png

Request body for data Message:
6128bba870871336608861.png

My manifest:
6128bc1bdc177923278366.png

Notification handler code:

class NotificationProvider {

  static NotificationProvider _instance;

  static final FirebaseMessaging _messaging = FirebaseMessaging();

  static String _fcmToken;
  static Function callback;

  static Notification _parseNotification(Map<String,dynamic> message){
    Notification data;
    if (Platform.isAndroid){
      data = Notification.fromJson(message,isIos: false);
    }
    else if (Platform.isIOS){
      data = Notification.fromJson(message,isIos: true);
    }
    return data;
  }

  static Future<dynamic> _onLaunchMessageHandler( Map<String,dynamic> message){
    final notification = _parseNotification(message);
    if (notification != null){
      callback.call(notification.header?.title ?? '');
    }
  }

  static Future<dynamic> _onMessageMessageHandler( Map<String,dynamic> message){
    final notification = _parseNotification(message);
    if (notification != null){
      callback.call(notification.header?.title ?? '');
    }
  }

  static Future<dynamic> _onResumeMessageHandler( Map<String,dynamic> message){
    final notification = _parseNotification(message);
    if (notification != null){
      callback.call(notification.header?.title ?? '');
    }
  }

  factory NotificationProvider(){
    return _instance ??= NotificationProvider._init();
  }

  NotificationProvider._init(){
    _messaging.configure(
      onMessage: _onMessageMessageHandler,
      onResume: _onResumeMessageHandler,
      onLaunch: _onLaunchMessageHandler,
    );
    if(Platform.isIOS){
      _messaging.requestNotificationPermissions();
    }
    _messaging.getToken().then((token) => _fcmToken = token);
  }

  Future<String> fcmToken() async {
    return _fcmToken ??= await _messaging.getToken();
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Popov, 2021-09-15
@vadimpopov94

When the application is closed (or in any other case), pressing the push will trigger one of these callbacks.

_messaging.configure(
   onMessage: _onMessageMessageHandler,
   onResume: _onResumeMessageHandler,
   onLaunch: _onLaunchMessageHandler,
 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question