make flutter notification (via firebase) pop to foreground (and not to be only icon in statusbar)

1,958

Looks like you are looking into how to get a heads-up notification on Android.

I would suggest to use FCM's "Data message" and then handle it at your FirebaseMessagingService.onMessageReceived implementation and then play there with priority/importance as it explained here.

Share:
1,958
Sebastian
Author by

Sebastian

Updated on December 13, 2022

Comments

  • Sebastian
    Sebastian over 1 year

    I am trying to implement notifications for a flutter app.

    On iPhone (left) it looks like expected and the notification is show for a short time, then hides automatically.

    But on Android (right side, Android 9, motorola) it appears just as an icon in the status bar. enter image description here How can i make it pop up? Right now I have to swipe down on it to see the contents.

    The notification is send via Firebase with their PHP-Sdk.

    $serviceAccount = ServiceAccount::fromJsonFile('....');
    
    $firebase = (new Factory)
        ->withServiceAccount($serviceAccount);
    $messaging = $firebase->createMessaging();
    
    $title = 'Test Titel '.date('YmdHis');
    $body = 'Meine Nachricht '.date('YmdHis');
    $colkey = 'newmessagtenoti';
    $count = 23;
    
    $message = new RawMessageFromArray([
            'notification' => [
                // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
                'title' => $title,
                'body' => $body,
            ],
            'data' => [
                'key_1' => 'Value 1',
                'key_2' => 'Value 2',
            ],
    
            'android' => [
                // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig
                'ttl' => '3600s',
                'priority' => 'high',
                "collapse_key"=> $colkey,
    
                'notification' => [
                    'notification_priority' => 'PRIORITY_MAX',
                    'visibility' => 'PUBLIC',
                    'title' => $title,
                    'body' => $body,
                    'notification_count' => $count,
                ],
            ],
            'apns' => [
                // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
                'headers' => [
                    'apns-priority' => '10',
                ],
                'payload' => [
                    'aps' => [
                        'alert' => [
                            'title' => $title,
                            'body' => $body,
                        ],
                        'badge' => $count,
                        'apns-collapse-id' =>  $colkey,
                    ],
                ],
            ],
    
        ]);
    
    // $firebase->getMessaging()->send($message);
    $report = $messaging->sendMulticast($message, $deviceTokens);
    
    echo 'Successful sends: '.$report->successes()->count().PHP_EOL;
    echo 'Failed sends: '.$report->failures()->count().PHP_EOL;
    
    if ($report->hasFailures()) {
        foreach ($report->failures()->getItems() as $failure) {
            echo $failure->error()->getMessage().PHP_EOL;
        }
    }
    

    I read all documentation but even with high priority it does not get bigger.

    I guess i am missing something in the code for Android. Maybe in "AndroidManifest.xml"?

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.flut7_push">
    
        <!-- io.flutter.app.FlutterApplication is an android.app.Application that
             calls FlutterMain.startInitialization(this); in its onCreate method.
             In most cases you can leave this as-is, but you if you want to provide
             additional functionality it is fine to subclass or reimplement
             FlutterApplication and put your custom class here. -->
        <application
            android:name="io.flutter.app.FlutterApplication"
            android:label="flut7_push"
            android:icon="@mipmap/ic_launcher">
            <activity
                android:name=".MainActivity"
                android:launchMode="singleTop"
                android:theme="@style/LaunchTheme"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize">
                <!-- This keeps the window background of the activity showing
                     until Flutter renders its first frame. It can be removed if
                     there is no splash screen (such as the default splash screen
                     defined in @style/LaunchTheme). -->
                <meta-data
                    android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                    android:value="true" />
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>            
                <intent-filter>
                    <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>            
            </activity>       
    
        </application>
    </manifest>