【问题标题】:FCM & Local notifications. Android works, on iOS notFCM 和本地通知。 Android 可以,iOS 不行
【发布时间】:2020-12-09 20:51:59
【问题描述】:

我正在使用具有 FCM 和本地通知的解决方案来接收频道上的特定推送通知。一切都适用于 Android。

我通过 Postman 将我的推送通知发送到 https://fcm.googleapis.com/fcm/send,一切正常。

但是,当我添加我的 iPhone 并完成所有需要的安装步骤时。我收到的唯一信息是控制台中的打印信息随消息飘动。我在 iPhone 上没有收到任何通知。

  final FirebaseMessaging _fcm = FirebaseMessaging();

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  var initializationSettingsAndroid;
  var initializationSettingsIOS;
  var initializationSettings;

  void _showNotification() async {
    //await _buildNotification();
  }

  Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    if (message.containsKey('data')) {
      // Handle data message
      final dynamic data = message['data'];
    }

    if (message.containsKey('notification')) {
      // Handle notification message

      final dynamic notification = message['notification'];
    }

    // Or do other work.
  }

  Future<void> _createNotificationChannel(
      String id, String name, String description) async {
    final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var androidNotificationChannel = AndroidNotificationChannel(
      id,
      name,
      description,
      importance: Importance.Max,
      playSound: true,
      sound: RawResourceAndroidNotificationSound('not_kiddin'),
      enableVibration: true,
    );
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(androidNotificationChannel);
  }

  Future<void> _buildNotification(String title, String body) async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'my_channel',
        'Channel Name',
        'Channel Description.',
        importance: Importance.Max,
        priority: Priority.High,
        playSound: true,
        enableVibration: true,
        sound: RawResourceAndroidNotificationSound('not_kiddin'),
        ticker: 'noorderlicht');
    var iOSChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSChannelSpecifics);

    await flutterLocalNotificationsPlugin.show(
        0, title, body, platformChannelSpecifics,
        payload: 'payload');
  }

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

    initializationSettingsAndroid =
        AndroidInitializationSettings('ic_launcher');
    initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);

    initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);

    _fcm.requestNotificationPermissions();

    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print(message);
        flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);

        //_showNotification();
        Map.from(message).map((key, value) {
          print(key);
          print(value);
          print(value['title']);
          _buildNotification(value['title'], value['body']);
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
      },
    );
  }

  Future onDidReceiveLocalNotification(
      int id, String title, String body, String payload) async {
    // display a dialog with the notification details, tap ok to go to another page
    showDialog(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: Text(title),
        content: Text(body),
        actions: [
          CupertinoDialogAction(
            isDefaultAction: true,
            child: Text('Ok'),
            onPressed: () {},
          )
        ],
      ),
    );
  }

  Future onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('Notification payload: $payload');
    }
  }

【问题讨论】:

  • 什么情况下你没有收到来自IOS的消息?在 IOS 上,当 App 处于前台时,FCM 不会显示横幅,它只会在 App 处于后台或已终止时显示
  • @thiennguyen 它永远不会收到消息,只有当应用程序处于前台时,当我的 iphone 连接到我的 mac 时,我才会在控制台中收到一些东西。但我从来没有收到过通知,当应用在后台时也没有。
  • 我也有同样的问题,你是怎么处理的?可以给我看吗?请

标签: android ios flutter firebase-cloud-messaging flutter-dependencies


【解决方案1】:

_fcm.requestNotificationPermissions();

您需要在 iOS 中请求权限(android 不需要)。

  _fcm.requestNotificationPermissions(
      const IosNotificationSettings(
          sound: true, badge: true, alert: true, provisional: true));

【讨论】:

    【解决方案2】:

    对于 IOS,FCM 消息具有不同的结构,在您的情况下,当平台是 IOS 时,请删除“数据”,如下所示。

    
                if(Platform.isAndroid){
                  roomId = message['data']['chatRoomId'];
                  senderId = message['data']['senderId'];
                }else if(Platform.isIOS){
                  roomId = message['chatRoomId'];
                  senderId = message['senderId'];
                }
    

    【讨论】:

    • 不确定你的意思是什么,你的意思是在 myBackgroundMessageHandler 未来?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2021-03-17
    • 2021-01-21
    • 2017-05-04
    相关资源
    最近更新 更多