flutter - 当应用程序关闭时在 Flutter 中推送通知

标签 flutter push-notification

我实现了推送通知(云消息传递),但是当应用程序完全关闭时我无法收到通知。
所以,我搜索了一下,发现我需要使用本地通知。
我开始使用本地通知,但我发现本地通知是基于用户方面的,例如由用户自己安排事件。所以,问题是如何使用flutter_local_notifications向所有用户发送通知?
谢谢

最佳答案

这个问题实际上分为两部分。

  • 应用关闭时未收到通知
  • 如何使用 flutter_local_notifications 向其他设备发送通知?包裹。

  • For the 'Not receiving' part:


    如果您已正确集成 FCM,则不必担心
    在后台或
    该应用程序已经被杀死。
    FCM 会自动为您处理推送通知,而您
    在后台或应用程序已经被杀死。
    只需确保您已将 FCM 正确集成到您的 flutter 中
    项目。
    关注此 link进行适当的整合。此外,iOS 集成有点棘手和冗长,请遵循 link用于 APNs 集成。
    如果您没有使用 FCM 正确设置 APN,Apple 将无法识别后台状态的推送通知触发器。

    Now, for the "Sending notification to other devices via flutter_local_notifications package:


    您不能通过将委托(delegate)给 FCM 的 flutter_local_notifications 推送通知。您必须发送 HTTP request通过 REST API 调用。
    示例代码如下:
    Future<Response> publishNotification() async {
      return await post(
        'https://fcm.googleapis.com/fcm/send',
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization': 'key=$firebaseServerKey',
        },
        body: jsonEncode(
          <String, dynamic>{
            "to": firebaseDeviceToken,
            'priority': 'high',
            "data": <String, dynamic>{
              'click_action': 'FLUTTER_NOTIFICATION_CLICK',
              "body": "Order#10",
              "title": "New Order",
              "status": "done",
              "screen": "NotificationPage",
              "description": "Order Details",
            }
          },
        ),
      );
    }
    
    您可能会添加我想向多个设备发送通知。
    为此,只需替换 tokenregistration_ids并将您的设备 token 列表放在 registration_ids .
    示例代码:
    Future<Response> publishNotification() async {
      return await post(
        'https://fcm.googleapis.com/fcm/send',
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization': 'key=$firebaseServerKey',
        },
        body: jsonEncode(
          <String, dynamic>{
            "registration_ids": ["token_1", "token_2", ...],
            'priority': 'high',
            "data": <String, dynamic>{
              'click_action': 'FLUTTER_NOTIFICATION_CLICK',
              "body": "Order#10",
              "title": "New Order",
              "status": "done",
              "screen": "NotificationPage",
              "description": "Order Details",
            }
          },
        ),
      );
    }
    
    我希望我能覆盖你。快乐编码:D

    关于flutter - 当应用程序关闭时在 Flutter 中推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66852722/

    相关文章:

    flutter - 如何在Flutter中创建自定义反向导航

    android - 如何将模式工作表顶部边缘更改为圆形不起作用

    firebase - 在flutter类中定义的 'SignInWithTwitter isn'方法

    dart - Flutter - 切换标签时保存交互或状态

    android - 我无法推送通知

    android - 如何在 ruby​​ 中使用 Appium 测试 android 和 iOS 中的推送通知

    ios - 本地推送通知可以有可变内容吗?

    ios - 如何在 completionHandler 中返回字符串值?

    flutter 提供程序 : How do I listen to a change of a class field inside a class field?

    android - Android 设备中用于推送通知的 iOS 设备 token 的替代方案是什么