【问题标题】:Flutter - How to display FCM notification outside notification bar like whatsappFlutter - 如何像 whatsapp 一样在通知栏外显示 FCM 通知
【发布时间】:2023-01-02 04:50:14
【问题描述】:

我使用 FCM 实现了 flutter 推送通知,它在应用程序处于前台、后台和应用程序关闭时完美工作。但是我正在寻找一种显示我的通知的方法,当你不在它的应用程序中时,Whatsapp 显示弹出通知的方式就像浮动在顶部(这次不在通知栏中。)见附图:

正如您在上图中看到的,用户在设备上的图库应用程序中,并且显示另一个应用程序的弹出通知消息。下面是我在 flutter 中的实现方式:

   FirebaseMessaging.instance.getInitialMessage().then((message) {

            print(message);
        });
   FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {

            print(message);
        });

FirebaseMessaging.onMessage.listen((RemoteMessage message) {

            print(message);
});

我的 android 清单如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app_name">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
 

    <application
        android:allowBackup="false"
        android:label="app_name"
        android:icon="@mipmap/ic_launcher">
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>

        <service
            android:name="MyNavigationService"
            android:foregroundServiceType="location" >
        </service>
        <receiver
            android:name="app_name.FirebaseBroadcastReceiver"
            android:exported="false"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </receiver>

        <!--service
            android:name="app_name.java.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service-->

        <service
            android:name="app_name.service.MyFirebaseInstanceIDService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

        <meta-data android:name="com.google.android.geo.API_KEY"
            android:value="A......"/>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:exported="true"
            android:showWhenLocked="false"
            android:turnScreenOn="false"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
       
            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background" />
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

            <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>
         <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我的 flutter pub 版本是:firebase_messaging: ^13.0.4

【问题讨论】:

  • 上面的问题有运气吗?
  • 我想你应该试试awesome_notifications

标签: android ios flutter firebase-cloud-messaging


【解决方案1】:

我能够解决一个解决方案,以防万一有人访问此页面寻求解决方案。我能够使用 awesome_notification 解决它@ 这是一个本地通知 pub.dev in flutter 我在我的FirebaseMessaging.onBackgroundMessage 中实现了它所以当应用程序在后台并且触发 FCM 然后调用未来函数包装很棒的通知如下所示:

在下面打电话

void main() async{

 FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

}

我的_firebaseMessagingBackgroundHandler 如下所示:


Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  debugPrint("mickeymueje");
  bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
 if (!isAllowed) return;


  await AwesomeNotifications().createNotification(
      content: NotificationContent(
          id: -1, // -1 is replaced by a random number
          channelKey: 'alerts',
          title: message.notification.title,
          body: message.notification.body,
          bigPicture: 'https://storage.googleapis.com/cms-storage-bucket/d406c736e7c4c57f5f61.png',
          largeIcon:'https://storage.googleapis.com/cms-storage-bucket/d406c736e7c4c57f5f61.png',
          notificationLayout: NotificationLayout.BigPicture,
          payload: {'notificationId': '1234567890'}),
      actionButtons: [
        NotificationActionButton(key: 'REDIRECT', label: 'Redirect'),

        NotificationActionButton(
            key: 'REPLY',
            label: 'Reply Message',
            requireInputText: true,
            actionType: ActionType.SilentAction
        ),
        NotificationActionButton(
            key: 'DISMISS',
            label: 'Dismiss',
            actionType: ActionType.DismissAction,
            isDangerousOption: true)
      ]
  );

}

请注意在main.dart中实现这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    相关资源
    最近更新 更多