Florian K
Florian K

Reputation: 2148

Push Notification received but not displayed (Flutter / Dart)

Notification in json is well received by the phone receiver and there is a print with json content in 'onMessage'. However no notifications is shown on the screen. Here is the json code sent :

    var client = http.Client();

    var jsonData = json.encode({
                "to": "/topics/$userId",
                "notification": {
                  "body": "$notificationText",
                  "title": "Title",
                },
                "data": {
                  "click_action": "FLUTTER_NOTIFICATION_CLICK",
                  "sound": "default",
                  "status": "done",
                  "screen": "ListPostsScreen",
                },
                "content_available": true,
                "priority": "high",
              });

              var headers = {
                'Authorization': "key=$key",
                'Content-Type': 'application/json',
              };

    var response = await client.post(url, headers: headers, body: jsonData);

    print(response.body);

firebaseMessaging in home screen :

final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );

This is the intent-filter in main/AndroidManifest.xml

<intent-filter>
  <action android:name="FLUTTER_NOTIFICATION_CLICK" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

My pubspec.yaml packages :

  firebase_messaging: ^5.1.6

Did I miss something ? Thank you.

Upvotes: 2

Views: 2675

Answers (1)

tyirvine
tyirvine

Reputation: 2341

This is a bit of a silly addition but I was having the same problem and realized my Do-Not-Disturb was turned on. I didn't think anything of it however because I had turned it off.

However, I guess because it was on a schedule it was still blocking notifications. I now have them working properly at the time of typing this - on iOS 13.5 displaying in the background.


Resources

https://fireship.io/lessons/flutter-push-notifications-fcm-guide/

https://pub.dev/packages/firebase_messaging

https://github.com/fireship-io/192-flutter-fcm-push-notifications/blob/master/lib/main.dart

Upvotes: 4

Related Questions