FCM onBackgroundMessage 未在后台颤动中调用

时间:2021-02-16 09:53:19

标签: flutter dart firebase-cloud-messaging

我已经有一段时间没有解决这个问题了。当应用程序在后台作为 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',
    );
  }

1 个答案:

答案 0 :(得分:0)

在 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>