【问题标题】:FCM onBackgroundMessage not getting called in background flutterFCM onBackgroundMessage 在后台颤动中没有被调用
【发布时间】:2021-05-19 04:09:12
【问题描述】:

我已经有一段时间没有解决方案了。当应用程序在后台 onBackgroundMessage 未被调用时,我无法处理通知。有什么问题?

Future<dynamic> _myBackgroundMessageHandler
    (Map<String, dynamic> message) async {
  print("onBackground Message called");
  return PushNotificationService().showNotification(message);
}


class PushNotificationService{
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  Future initialize(context) async{
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        fetchRideInfo(message['data']['orderId'], context, "onMessage");
      },
    onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,

      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        fetchRideInfo(message['data']['orderId'], context, "onResume");
      },
    );
  }

  Future showNotification(Map<String, dynamic> message) async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'channel id',
      'channel name',
      'channel desc',
      importance: Importance.max,
      priority: Priority.high,
    );

    var platformChannelSpecifics =
    new NotificationDetails(android: androidPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
      0,
      'new message arived',
      'i want ${message['data']}',
      platformChannelSpecifics,
      payload: 'Default_Sound',
    );
  }

【问题讨论】:

  • 在 Android 或 iOS 中?
  • 只是安卓兄弟

标签: flutter dart firebase-cloud-messaging


【解决方案1】:

在 android/app/build.gradle 添加依赖项 com.google.firebase:firebase-messaging:20.3.0

dependencies {
    implementation 'com.google.firebase:firebase-messaging:20.3.0'
}

然后创建一个名为 Application.kt 的类 注意:如果你想用 FCM 运行一些东西,那么你也必须注册它。在这种情况下,我使用 flutter_local_notifications 处理后台通知。如果您有另一个,请注册那个并且不要忘记修复导入。

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"));
        }
  }
    }

然后在 AndroidManifest.xml 中注册。

AndroidManifest.xml
     <application
           android:label="@string/app_name"
           android:name=".Application"
    
           >
.....
 </application>

【讨论】:

  • 我明白了:应用程序必须扩展 android.app.Application
  • 不工作,兄弟。 _myBackgroundMessageHandler 没有被调用
猜你喜欢
  • 2021-03-08
  • 2021-04-05
  • 2021-05-16
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 2021-04-28
  • 2018-04-12
  • 2020-01-23
相关资源
最近更新 更多