首页 > 解决方案 > 如何在 Flutter 上使用 OneSingal 过滤某些通知?

问题描述

我将 OneSignal SDK ( onesignal: ^1.1.0) 与 Flutter 一起使用。我需要为我的应用程序上的每个用户过滤一些类型/类别的通知。是否有任何类型的事件可用于决定是否显示通知?

我的想法是创建不同的通知类别。每个用户都可以选择他/她可以接收的通知类型。

目前我只能选择是否接收所有通知调用setSubscription

OneSignal.shared.setSubscription(myBool);

如果 value 设置为 true,则用户可以收到通知,如果设置为 false,他/她将无法收到通知。

这是我的main.dart代码:

import 'package:flutter/material.dart';
import 'package:onesignal/onesignal.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  bool _areNotificationsEnabled = true;

  @override
  void initState() {
    super.initState();
    OneSignal.shared.init('myKey', iOSSettings: {
      OSiOSSettings.autoPrompt: false,
      OSiOSSettings.inAppLaunchUrl: true
    });

    OneSignal.shared
        .setInFocusDisplayType(OSNotificationDisplayType.notification);
    OneSignal.shared.setNotificationOpenedHandler(
        (OSNotificationOpenedResult handler) async {
      print('Notification opened');
      print(handler.notification.payload.additionalData.toString());
    });
    OneSignal.shared
        .setNotificationReceivedHandler((OSNotification notification) async {
      print("Notification received");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: SwitchListTile(
          value: _areNotificationsEnabled,
          title: Text('Enable notifications'),
          subtitle: Text('Turn on or off your notifications'),
          onChanged: (bool newValue) {
            OneSignal.shared.setSubscription(newValue);
            print(
                'Notifications state --> ${newValue ? 'Enabled' : 'Disabled'}');
            setState(() {
              _areNotificationsEnabled = !_areNotificationsEnabled;
            });
          },
        ),
      ),
    );
  }
}

标签: flutterdartonesignal

解决方案


HowdyE.Benedos,您可以使用数据标签根据用户的偏好对用户进行细分。

设置数据标签的能力非常强大。您可以在每个用户上存储数据属性。标签适用于所有 OneSignal 帐户,是我们客户中非常受欢迎的功能。用户被标记有他们想要接收的消息类型、内容类别、购物车放弃信息、订阅状态和用户偏好等属性。

在此处阅读更多信息:https ://onesignal.com/blog/data-integration-and-tagging-using-onesignal/


推荐阅读