简体   繁体   English

Flutter onBackgroundMessage android myimage 通知不起作用

[英]Flutter onBackgroundMessage android myimage notification not working

Flutter firebase background notification I pushed the image notification but it's showing only text but if app open it's working fine but app terminated or minimize it's showing only default notification I tried everything but it doesn't work I referred this also https://pub.dev/packages/firebase_messaging no use. Flutter firebase 背景通知 我推送了图像通知,但它只显示文本,但如果应用程序打开它工作正常,但应用程序终止或最小化它只显示默认通知 我尝试了一切,但它不起作用 我也提到了这个Z5E056C500A1C4B6 。 dev/packages/firebase_messaging没用。

check this image检查这张图片

background notification not showing that image?背景通知没有显示该图像?

Application.kt应用程序.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 MainActivity.kt

 package YOUR PACKAGE

 import io.flutter.embedding.android.FlutterActivity

 class MainActivity: FlutterActivity() {
 }

AndroidManifest.xml 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 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);
      },
    );

myBackgroundMessageHandler method in same main.dart同一 main.dart 中的 myBackgroundMessageHandler 方法

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

Firebase send notification json Rest API 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"
        }

To show images you may need to use image tag inside notification in your Firebase send notification json Rest API .要显示图像,您可能需要在Firebase 发送通知 json Rest ZDB974278714CA3ACE1408中的通知中使用图像标签。 The below code worked for me.下面的代码对我有用。

     {          
         "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"
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM