点击通知时 Flutter OneSignal 不会打开应用程序

发布于 2025-01-11 03:34:52 字数 1434 浏览 2 评论 0原文

我有 Flutter 应用程序,它可以与 OneSignal 插件配合使用,但有一个问题,当单击通知时,如果应用程序已关闭或在后台运行,我将无法打开应用程序,但在前台时,它会毫无问题地打开通知。我想在收到通知时打开应用程序,然后单击它。

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

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

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  static const String oneSignalAppId = "XXXXXX-XXXXX-XXXXXXX-XXXXXX-XXXXX";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Manchester United"),
        elevation: 0,
      ),
      backgroundColor: Colors.red[200],
      body: const WebView(
        initialUrl: 'https://manchester-united.football/',
        javascriptMode: JavascriptMode.unrestricted,
      ),
    );
  }

  Future<void> initPlatformState() async {
    OneSignal.shared.setAppId(
      oneSignalAppId,
    ); //OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);
    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
      var data = result.notification.additionalData;

      globals.appNavigator?.currentState?.push(MaterialPageRoute(
        builder: (context) => SecondPage(
          postId: data!["myappurl"].toString(),
        ),
      ));
    });
  }
}

I have Flutter app, which works fine with OneSignal plugin, but i have one issue, when click on notification i can not open app if app is closed or runs background, but when forground it opens notification without issue. I want to open app when notification comes, and i wclick on it.

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

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

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  static const String oneSignalAppId = "XXXXXX-XXXXX-XXXXXXX-XXXXXX-XXXXX";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Manchester United"),
        elevation: 0,
      ),
      backgroundColor: Colors.red[200],
      body: const WebView(
        initialUrl: 'https://manchester-united.football/',
        javascriptMode: JavascriptMode.unrestricted,
      ),
    );
  }

  Future<void> initPlatformState() async {
    OneSignal.shared.setAppId(
      oneSignalAppId,
    ); //OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);
    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
      var data = result.notification.additionalData;

      globals.appNavigator?.currentState?.push(MaterialPageRoute(
        builder: (context) => SecondPage(
          postId: data!["myappurl"].toString(),
        ),
      ));
    });
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文