Flutter onBackgroundMessage android myimage通知不起作用

Posted

技术标签:

【中文标题】Flutter onBackgroundMessage android myimage通知不起作用【英文标题】:Flutter onBackgroundMessage android myimage notification not working 【发布时间】:2020-09-18 01:28:43 【问题描述】:

Flutter firebase 背景通知我推送了图像通知,但它只显示文本,但如果应用程序打开它工作正常但应用程序终止或最小化它只显示默认通知我尝试了一切但它不起作用我也提到了这个 https://pub.dev/packages/firebase_messaging没用。

check this image

背景通知未显示该图像?

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>

ma​​in.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"
        

【问题讨论】:

"寻求调试帮助的问题('为什么这段代码不工作?')必须包括期望的行为、特定问题或错误必要的最短代码 在问题本身。没有明确的问题陈述的问题对其他读者没有用处。请参阅:@ 987654323@" @ManikandanM 你可以编辑问题,如果你想清楚,干杯 【参考方案1】:

要显示图像,您可能需要在 Firebase 发送通知 json Rest API 中使用图像标记inside notification。下面的代码对我有用。

“通知”: "title":"计划已过期", "body":"您的计划已过期,请立即升级您的计划", “图像”:“https://imgsv.imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg” "to":"NOTIFICATION_TOKEN"

【讨论】:

以上是关于Flutter onBackgroundMessage android myimage通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章

[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...

flutter 日志输出,Flutter打印日志,flutter log,flutter 真机日志

Flutter开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )

flutter与原生混编(iOS)

Flutter-布局

如何解决flutter gradle build error?C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991