Firebase 通知不会发出声音或振动

Firebase notification won`t sound or vibrate flutter

所以我在我的 futter 应用程序上启用了 firebase 消息传递,一切正常,但是,当我在我的应用程序中收到通知时,它不会发出声音或振动,android 或 ios。我相信 FCM 默认会这样做,我也有 "IosNotificationSettings(sound: true, badge: true, alert: true)",但通知只是静音。你们知道会发生什么吗?我搜索了这种情况,但找不到任何相关信息。在此先感谢您的帮助。

class _HomeScreenState extends State<HomeScreen>
with SingleTickerProviderStateMixin {

 String _homeScreenText = "Waiting for token...";

 final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

 static const String contato = "contato";

 TabController _tabController;

 @override
 void initState() {
    _tabController = new TabController(length: 5, vsync: this);
    super.initState();

    _firebaseMessaging.configure(
       onMessage: (Map<String, dynamic> message) {
         print('on message $message');
       },
       onResume: (Map<String, dynamic> message) {
         print('on resume $message');
       },    
       onLaunch: (Map<String, dynamic> message) {
       print('on launch $message');
       },
      );

     _firebaseMessaging.requestNotificationPermissions(
         const IosNotificationSettings(sound: true, badge: true, alert: true));
     _firebaseMessaging.onIosSettingsRegistered
       .listen((IosNotificationSettings settings) {
         print("Settings registered: $settings");
      });
     _firebaseMessaging.getToken().then((String token) {
       assert(token != null);
       setState(() {
     _homeScreenText = "Push Messaging token: $token";
     });
     print(_homeScreenText);
     });

     _firebaseMessaging.getToken().then((token) {
     Firestore.instance.collection("pushtokens").document().setData({"devtoken": token});
     });
    }

要在通知中启用声音,您需要在通知数据中添加 sound: default 部分,如下所示:

{
  data: {
    google.sent_time: 1588863942303, 
    click_action: FLUTTER_NOTIFICATION_CLICK, 
    google.original_priority: high,
    collapse_key: YourPackageName,
    google.delivered_priority: high,
    sound: default,
    from: YourSenderId,
    google.message_id: 0:1588863942508709%a91ceea4a91ceea4,
    google.ttl: 60
  },
  notification: {}
}

您可以为 http 请求添加类似这样的内容以发送通知。

但您首先需要检查的是您的 phone 是否允许应用程序通知。 这让我浪费了大部分时间。

{
    topic: topic,
    android: {
      priority: "high",
      notification: {
        defaultSound: true,
        //sound: "default",
      },
    },
    data: {
      somedata: "value",
    },
    notification: {
      title: title,
      body: description,
      imageUrl: "https://i.picsum.photos/id/999/536/354.jpg?hmac=xYKikWHOVjOpBeVAsIlSzDv9J0UYTj_tNODJCKJsDo4",
    },
}