颤动:未调用后台消息处理程序

时间:2020-06-01 17:54:27

标签: flutter dart push-notification firebase-cloud-messaging

我已经在我的应用程序中实现了Firebase云消息传递和firebase功能,并使用了抖动的本地通知,并添加了showNotifications函数来制作频道ID并更改其重要性和优先级,以便我在抬头时收到通知(横幅),但对于某些情况我从Firestore发送消息时未调用onbackgroundmessagehandler回调的原因,

这是我的代码:

  Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
      print('background message handler');
      return await _TabsScreenState()._showNotification(message);
    }

    class TabsScreen extends StatefulWidget {
      static const String id = '/tabs screen';


      @override
      _TabsScreenState createState() => _TabsScreenState();
    }

    class _TabsScreenState extends State<TabsScreen> {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();
      FirebaseMessaging _fcm = FirebaseMessaging();
      Firestore _fireStore = Firestore.instance;

      void _onSaved() async {
        String myToken = await _fcm.getToken();

        if (myToken != null) {
          var tokenRef = _fireStore.collection('tokens').document(myToken);
          print(myToken);

          await tokenRef.setData({
            'token': myToken,
            'createdAt': DateTime.now(),
            'platform': Platform.operatingSystem,
          });
        }
      }

      Future _showNotification(Map<String, dynamic> message) async {
        print('show notifications');
        var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
          'channel id',
          'channel name',
          'channel desc',
          importance: Importance.Max,
          priority: Priority.High,
        );

        var platformChannelSpecifics =
            new NotificationDetails(androidPlatformChannelSpecifics, null);
        await flutterLocalNotificationsPlugin.show(
          0,
          message['notifications']['title'],
          'hi',
          platformChannelSpecifics,
          payload: 'default sound',
        );
      }

      Future selectNotification(String payload) async {
        await flutterLocalNotificationsPlugin.cancelAll();
      }


      void initState() {
        var initializationSettingsAndroid =
            AndroidInitializationSettings('@mipmap/ic_launcher');
        var initializationSettings =
            InitializationSettings(initializationSettingsAndroid, null);
        flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: selectNotification);
        _onSaved();
        _fcm.configure(
            onBackgroundMessage: myBackgroundMessageHandler,
            onMessage: (Map<String, dynamic> message) async {
              print("On Message: $message");
              print(message['data']['message']);
              showDialog(
                context: context,
                builder: (ctx) {
                  return AlertDialog(
                    content: ListTile(
                      title: Text(message['data']['message']),
                    ),
                    actions: <Widget>[
                      FlatButton(
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                        child: Text('Ok'),
                      )
                    ],
                  );
                },
              );
            },
            onResume: (Map<String, dynamic> resume) async {
              print("on resume: $resume");
            },
            onLaunch: (Map<String, dynamic> launch) async {
              print("on launch: $launch");
            });



      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
          title: Text("My App"),
        );
      }
    }

这是我的andoird清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tamataapp">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. "io.flutter.app.FlutterApplication" -->
    <application
        android:name=".Application"
        android:label="tamataapp"
        android:icon="@mipmap/ic_launcher">
        <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">
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="default_notification_channel_id”>Channel ID"/>
            <!-- 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"/>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <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="flutterEmbedding"
            android:value="2" />
        <service
            android:name=".java.MyFirebaseMessagingService"
            android:exported="false">
        </service>
    </application>
</manifest>

这是我的index.js代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

var newData;

exports.messageTrigger = functions.firestore.document('messages/{messagesId}').onCreate(async (snapshot, context) => {


    if (snapshot.empty) {
        console.log('No Devices');
        return;
    }

    newData = snapshot.data();

    const deviceIdTokens = await admin
        .firestore()
        .collection('tokens')
        .get();

    var tokenm = [];

    for (var one of deviceIdTokens.docs) {
        tokenm.push(one.data().token);
    }
    const payload = {
        data: {
          click_action: 'FLUTTER_NOTIFICATION_CLICK',
          message: newData.message,

        }
      };



    try {
        const response = await admin.messaging().sendToDevice(tokenm, payload);
        console.log('Notification sent successfully');
    } catch (err) {
        console.log(err);
    }
});

2 个答案:

答案 0 :(得分:0)

您正在使用哪个版本的firebase_messaging?

在发布商页面https://pub.dev/packages/flutter_local_notifications中:

与firebase_messaging的兼容性

以前,存在导致此插件无法正常工作的问题 正确地使用firebase_messaging插件。这意味着回调 来自每个插件的调用可能不会被调用。的6.0.13版 firebase_messaging应该可以解决此问题,所以请碰碰您的 firebase_messaging依赖项,并按照 firebase_messaging的自述文件。

答案 1 :(得分:0)

onBackgroundMessage在JSON中发送notification时不起作用,因此请从JSON中删除notification以使onBackgroundMessage起作用。

如果在JSON中设置了notifications,您将在移动设备中看到默认通知(抬头)(横幅),并且onBackgroundMessage将不会运行。