Flutter onBackgroundMessage android myimage通知无法正常运行

时间:2020-05-30 14:04:06

标签: android flutter firebase-cloud-messaging firebase-notifications

Flutter Firebase背景通知我推送了图像通知,但它仅显示文本,但是如果应用程序打开它可以正常工作,但应用程序终止或最小化,它仅显示默认通知,我尝试了所有尝试,但不起作用,我也提到了{{ }}没用。

https://pub.dev/packages/firebase_messaging

后台通知未显示该图片?

Application.kt

package YOUR PACKAGE

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
        if (!registry!!.hasPlugin("com.dexterous.flutterlocalnotifications")) {
            FlutterLocalNotificationsPlugin.registerWith(registry!!.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
        }
    }
}

MainActivity.kt

 package YOUR PACKAGE

 import io.flutter.embedding.android.FlutterActivity

 class MainActivity: FlutterActivity() {
 }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR PACKAGE"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:name=".Application"
        android:label="APP_NAME"
        android:icon="@drawable/home_logo"
        tools:replace="android:label">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!-- Add below to ensure we get the payload when tapping on a notification -->
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
             <meta-data
                android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="@string/default_notification_channel_id"/>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

</manifest>

main.dart

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
        showNotification(message);
      },
      onBackgroundMessage: Platform.isIOS
          ? null
          : myBackgroundMessageHandler,
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');
        showNotification(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
        showNotification(message);
      },
    );
在同一main.dart中的

myBackgroundMessageHandler方法

 static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    final notification = message['notification'];
    final data = message['data'];
    print(notification);        
  }

Firebase发送通知json Rest API

    #header:
        Content-Type:application/json
        Authorization:key=YOUR FIREBASE SERVER KEY
    #body -> raw
        {
          "notification":{
          "title":"Plan Expired",
          "body":"Your plan has expired please upgrade your plan today"
           },
           "data": {
                "image":"https://i.ytimg.com/vi/zZ72Ujn8Rfw/maxresdefault.jpg"
           },
          "to":"NOTIFICATION TOKEN"
        }

1 个答案:

答案 0 :(得分:0)

要显示图像,您可能需要在 Firebase发送通知json Rest API 中使用图像标签内部通知。以下代码对我有用。

     {          
         "notification":{
          "title":"Plan Expired",
          "body":"Your plan has expired please upgrade your plan today",
          "image":"https://imgsv.imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg"
           }
          "to":"NOTIFICATION_TOKEN"
        }