Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase消息传递-当应用程序关闭或休眠时,不会发送通知_Firebase_Flutter_Google Cloud Firestore_Firebase Cloud Messaging - Fatal编程技术网

Firebase消息传递-当应用程序关闭或休眠时,不会发送通知

Firebase消息传递-当应用程序关闭或休眠时,不会发送通知,firebase,flutter,google-cloud-firestore,firebase-cloud-messaging,Firebase,Flutter,Google Cloud Firestore,Firebase Cloud Messaging,我已经在Flatter中构建了我的应用程序,并且实现了LocalNotifications和FCM消息 这是我的代码: final FirebaseMessaging firebaseMessaging = FirebaseMessaging(); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); @overr

我已经在Flatter中构建了我的应用程序,并且实现了LocalNotifications和FCM消息

这是我的代码:

 final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  FlutterLocalNotificationsPlugin();


  
  @override
  void initState() {
    super.initState();
    registerNotification();
    configLocalNotification();

  }



  void registerNotification() {
    firebaseMessaging.requestNotificationPermissions();

    firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
      print('onMessage: $message');
      return ;
    }, onResume: (Map<String, dynamic> message) {
      print('onResume: $message');
      return Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => NotificationsScreen()));
    },
        onLaunch: (Map<String, dynamic> message) {
      print('onLaunch: $message');
      return;
    });

    firebaseMessaging.getToken().then((token) {
      print('token: $token');
      FirebaseFirestore.instance
          .collection('Consultant')
          .doc(firebaseUser.uid)
          .update({'deviceToken': token});
    }).catchError((err) {
      //Fluttertoast.showToast(msg: err.message.toString());
    });
  }

  Future selectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: $payload');
    }
    await Navigator.push(
      context,
      MaterialPageRoute<void>(builder: (context) => NotificationsScreen(payload: payload,)),
    );
  }

  void showNotification(message) async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      Platform.isAndroid
          ? 'it.wytex.vibeland_pro_app'
          : 'it.wytex.vibeland_pro_app',
      'Vibeland Pro',
      'Vibeland Pro',
      playSound: true,
      enableVibration: true,
      importance: Importance.max,
      priority: Priority.high,
    );
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);

    print(message);
    print(message['body'].toString());
    print(json.encode(message));

    await flutterLocalNotificationsPlugin.show(0, message['title'].toString(),
        message['body'].toString(), platformChannelSpecifics,
        payload: json.encode(message));

    await flutterLocalNotificationsPlugin.show(
      1, 'As you can on the FlutterFire documentation, foreground and background notification are handled differently by the plugin, so there are 2 thing you need to fix in your app.

First you need to prepare your Cloud Function to send background notifications as well as foreground, and in order to to that, you need to prepare your json to not only have a
notification
but also a
data
field, as follows:

const payload = {
    notification: {
      title: "" + name,
      body: "" + subject,
      sound: "default",
    },  
    data: {
        click_action: "FLUTTER_NOTIFICATION_CLICK"
    }
};
final FirebaseMessaging FirebaseMessaging=FirebaseMessaging();
最终颤振局部通知图京颤振局部通知图京=
flatterLocalNotificationsPlugin();
@凌驾
void initState(){
super.initState();
注册通知();
configLocalNotification();
}
无效注册表通知(){
firebaseMessaging.requestNotificationPermissions();
配置(onMessage:(映射消息){
打印('onMessage:$message');
返回;
},onResume:(映射消息){
打印('onResume:$message');
返回导航器.push(
上下文
材料路线(
生成器:(上下文)=>NotificationsScreen());
},
onLaunch:(映射消息){
打印('onLaunch:$message');
返回;
});
firebaseMessaging.getToken().then((令牌){
打印('token:$token');
FirebaseFirestore.instance
.收款(“顾问”)
.doc(firebaseUser.uid)
.update({'deviceToken':标记});
}).catchError((err){
//flatterToast.showtoos(msg:err.message.toString());
});
}
未来选择通知(字符串负载)异步{
如果(有效负载!=null){
debugPrint('notification payload:$payload');
}
等待导航器(
上下文
MaterialPage路由(生成器:(上下文)=>NotificationsScreen(有效载荷:有效载荷,),
);
}
无效显示通知(消息)异步{
var androidPlatformChannelSpecifics=新的AndroidNotificationDetails(
平顶板蓝根
?it.wytex.vibeland_pro_应用程序'
:“it.wytex.vibeland_pro_应用程序”,
“Vibeland Pro”,
“Vibeland Pro”,
playSound:没错,
使能振动:正确,
重要性:重要性.max,
优先级:优先级,高,
);
var iOSPlatformChannelSpecifics=新的IOSNotificationDetails();
var platformChannelSpecifics=新通知详细信息(
android:androidPlatformChannelSpecifics,iOS:iOSPlatformChannelSpecifics);
打印(信息);
打印(消息['body'].toString());
打印(json.encode(消息));
等待LocalNotificationsPlugin.show(0,消息['title'].toString(),
消息['body'].toString(),平台通道详细信息,
有效负载:json.encode(message));
等待本地通知。显示(
1、正如您在应用程序中所能做的那样,插件对前台和后台通知的处理方式有所不同,因此您需要在应用程序中修复两件事

首先,您需要准备云函数来发送后台通知和前台通知,为了实现这一点,您需要准备json,使其不仅具有
通知
,而且还具有
数据
字段,如下所示:

firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
        print('onMessage: $message');
        return ;
    }, 
    onResume: (Map<String, dynamic> message) {
        print('onResume: $message');
        return Navigator.push(
            context,
            MaterialPageRoute(
            builder: (context) => NotificationsScreen()));
    },
    onLaunch: (Map<String, dynamic> message) {
        print('onLaunch: $message');
        return;
    },
    onBackgroundMessage: myBackgroundMessageHandler
);
其次,您需要配置firebaseMassaging以接收背景消息,如下所示:

Future<void> myBackgroundMessageHandler(RemoteMessage message) async {        
    print("Handling a background message: ${message}");
}
onBackgroundMessage: myBackgroundMessageHandler
Future<void> myBackgroundMessageHandler(RemoteMessage message) async {        
    print("Handling a background message: ${message}");
}

firebaseMessaging.configure(
onMessage:(映射消息){
打印('onMessage:$message');
返回;
}, 
onResume:(映射消息){
打印('onResume:$message');
返回导航器.push(
上下文
材料路线(
生成器:(上下文)=>NotificationsScreen());
},
onLaunch:(映射消息){
打印('onLaunch:$message');
返回;
},
onBackgroundMessage:myBackgroundMessageHandler
);
最后,您需要创建一个手动处理后台消息的处理程序,按照文档中的示例,您可以执行以下操作:

Future<void> myBackgroundMessageHandler(RemoteMessage message) async {        
    print("Handling a background message: ${message}");
}
onBackgroundMessage: myBackgroundMessageHandler
Future<void> myBackgroundMessageHandler(RemoteMessage message) async {        
    print("Handling a background message: ${message}");
}

Future myBackgroundMessageHandler(RemoteMessage消息)异步{
打印(“处理背景消息:${message}”);
}
添加行:

Nexus, [10.03.21 16:59]
[ File : app-release.apk ]

Amore, [10.03.21 17:09]
java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
 at android.app.ActivityThread.handleCreateService(ActivityThread.java:4023)
 at android.app.ActivityThread.access$1600(ActivityThread.java:224)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1903)
 at android.os.Handler.dispatchMessage(Handler.java:107)
 at android.os.Looper.loop(Looper.java:224)
 at android.app.ActivityThread.main(ActivityThread.java:7562)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
 at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.C(Unknown Source:70)
 at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(Unknown Source:40)
 at android.app.ActivityThread.handleCreateService(ActivityThread.java:4011)
 ... 8 more

你认为有必要广告:Future myBackgroundMessageHandler(RemoteMessage message)async{print(“处理背景消息:${message}”);Wait Wait Firebase.initializeApp();//这一行???}如果应用程序已经初始化,我认为是这样。太好了,你救了我的应用程序和我的命!很高兴能提供帮助:)请阅读我的评论,因为我遇到了应用程序崩溃和错误:尝试执行建议的操作也可以,导入
io.flatter.plugin.common.PluginRegistry.PluginRegistry.PluginRegistrantCallback;
并将此代码添加到您的
onCreate
-
flatterfirebasemessagingservice.setPluginRegistrant(this);