【发布时间】:2020-09-18 01:28:43
【问题描述】:
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"
}
【问题讨论】:
-
"寻求调试帮助的问题('为什么这段代码不工作?')必须包括期望的行为、特定问题或错误和必要的最短代码 在问题本身。没有明确的问题陈述的问题对其他读者没有用处。请参阅:@ 987654323@"
-
@ManikandanM 你可以编辑问题,如果你想清楚,干杯
标签: android flutter firebase-cloud-messaging firebase-notifications