使颤动通知(通过firebase)弹出到前台(并且不仅仅是状态栏中的图标)

Posted

技术标签:

【中文标题】使颤动通知(通过firebase)弹出到前台(并且不仅仅是状态栏中的图标)【英文标题】:make flutter notification (via firebase) pop to foreground (and not to be only icon in statusbar) 【发布时间】:2020-01-10 23:36:27 【问题描述】:

我正在尝试为颤振应用实现通知。

在 iPhone(左)上,它看起来像预期的那样,通知会显示一小段时间,然后自动隐藏。

但在 android(右侧,Android 9,摩托罗​​拉)上,它只是在状态栏中显示为一个图标。 我怎样才能让它弹出?现在我必须向下滑动才能看到内容。

通知是通过 Firebase 及其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;
    

我阅读了所有documentation,但即使具有高优先级,它也不会变得更大。

我想我在 Android 的代码中遗漏了一些东西。也许在“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>

【问题讨论】:

我也有同样的问题... 【参考方案1】:

您似乎正在研究如何在 Android 上获取heads-up notification。

我建议使用FCM's "Data message",然后在您的FirebaseMessagingService.onMessageReceived 实现中处理它,然后按照here 的解释以优先级/重要性在那里播放。

【讨论】:

以上是关于使颤动通知(通过firebase)弹出到前台(并且不仅仅是状态栏中的图标)的主要内容,如果未能解决你的问题,请参考以下文章

Web 前台推送通知未弹出在 Reactjs 中使用 Firebase 集成

Flutter Firebase如何控制通知在应用程序处于前台时不显示

启用或禁用通知颤动

颤动的firebase推送通知图标

Firebase Messaging Unity 插件仅在前台显示通知

颤动中特定设备的Firebase消息通知