Flutter推送通知插件notificare_push的使用

Flutter推送通知插件notificare_push的使用


Notificare Flutter SDK

GitHub release License

Notificare Flutter SDK 可以让您快速而简便地与许多 Notificare API 服务进行通信,并使您可以无缝集成各种功能,从推送通知到上下文存储。

开始使用我们的 📚 集成指南示例项目

目录

功能

推送通知

接收推送通知并自动跟踪其参与度。

推送通知UI

使用原生屏幕和元素显示您的推送通知,并且无需努力即可处理其操作。

应用内消息

无需努力即可自动向用户显示相关应用内内容。

收件箱

具有内置消息收件箱的应用程序由于其性质可以保留消息,用户可以随时打开这些消息,因此转换率更高。SDK 提供了构建收件箱界面所需的所有工具。

地理位置

将用户的地理位置转化为相关信息,自动化基于地理位置行为对用户进行分段,并创建真正具有情境的通知。

忠诚度

利用数字卡的力量,这些卡片可以在您的应用程序之外生存,并始终在客户口袋里。

资产

为您的应用程序添加强大的上下文营销功能。在正确的时间或地点向正确的用户展示正确的内容。最大化您已经创建的内容,而不会增加开发成本。

可扫描对象

通过扫描 NFC 标签或二维码解锁新内容,这些标签或二维码可以无缝集成到您的移动应用程序中。

安装

要求

  • Android 6(API 级别 23)及以上版本
  • iOS 11 及以上版本

配置

将 Flutter 包添加到您的 pubspec.yaml 文件,并遵循入门指南。

dependencies:
  # 必需
  notificare: ^4.0.0

  # 可选模块
  notificare_assets: ^4.0.0
  notificare_geo: ^4.0.0
  notificare_inbox: ^4.0.0
  notificare_loyalty: ^4.0.0
  notificare_push: ^4.0.0
  notificare_push_ui: ^4.0.0
  notificare_scannables: ^4.0.0

入门

集成

开始使用我们的 📚 集成指南示例项目

示例

  • 示例项目 示例项目 以简化的方式演示其他集成,以便快速了解如何实现给定的功能。

示例代码

以下是一个完整的示例,展示了如何使用 notificare_push 插件来接收推送通知。

首先,在 pubspec.yaml 中添加依赖项:

dependencies:
  notificare_push: ^4.0.0

然后,运行 flutter pub get 更新依赖项。

接下来,初始化 Notificare SDK 并注册推送通知。

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Notificare Push Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  void initState() {
    super.initState();

    // 初始化 Notificare SDK
    Notificare.init().then((_) {
      print('Notificare SDK initialized successfully.');

      // 注册推送通知
      Notificare.registerForPushNotifications().then((token) {
        print('Push notification token: $token');
      }).catchError((error) {
        print('Failed to register for push notifications: $error');
      });
    }).catchError((error) {
      print('Failed to initialize Notificare SDK: $error');
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Notificare Push Demo'),
      ),
      body: Center(
        child: Text('Hello, Notificare!'),
      ),
    );
  }
}

更多关于Flutter推送通知插件notificare_push的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter推送通知插件notificare_push的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用notificare_push插件来处理推送通知的代码示例。这个示例包括基本的配置和接收推送通知的回调处理。

首先,确保你的Flutter项目已经创建,并且你已经添加了notificare_push插件到你的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  notificare_push: ^最新版本号  # 请替换为实际的最新版本号

然后,运行flutter pub get来安装依赖。

配置AndroidManifest.xmlInfo.plist

对于Android,你可能需要在AndroidManifest.xml中配置一些权限和接收器。对于iOS,你可能需要在Info.plist中添加一些配置。然而,notificare_push插件通常会提供详细的文档来指导你进行这些配置。这里假设你已经完成了这些配置。

初始化Notificare Push

在你的Flutter应用的主入口文件(通常是main.dart)中,初始化NotificarePush插件。

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Notificare Push Example'),
        ),
        body: PushSetup(),
      ),
    );
  }
}

class PushSetup extends StatefulWidget {
  @override
  _PushSetupState createState() => _PushSetupState();
}

class _PushSetupState extends State<PushSetup> {
  @override
  void initState() {
    super.initState();
    initNotificarePush();
  }

  void initNotificarePush() async {
    // 初始化Notificare Push
    try {
      await NotificarePush.initialize(
        apiKey: "YOUR_NOTIFICARE_API_KEY",  // 替换为你的Notificare API Key
        environment: "production",  // 或 "development"
        onNotificationReceived: (notification) async {
          // 当接收到通知时调用
          print("Notification received: ${notification.toMap()}");
          // 这里可以处理通知,比如显示一个本地通知
          showLocalNotification(notification);
        },
        onNotificationOpened: (notification) async {
          // 当用户点击通知时调用
          print("Notification opened: ${notification.toMap()}");
          // 处理通知点击事件
        },
      );
    } catch (e) {
      print("Failed to initialize Notificare Push: $e");
    }
  }

  void showLocalNotification(NotificareNotification notification) {
    // 这里你可以使用其他本地通知插件来显示通知
    // 例如 flutter_local_notifications 插件
    // 注意:你需要先添加flutter_local_notifications到你的pubspec.yaml并导入
    // import 'package:flutter_local_notifications/flutter_local_notifications.dart';

    // FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    //     FlutterLocalNotificationsPlugin();
    // var androidChannelSpecifics = AndroidNotificationDetails(
    //   'your_channel_id',
    //   'Your channel name',
    //   'Your channel description',
    //   importance: Importance.Max,
    //   priority: Priority.High,
    // );
    // var iOSChannelSpecifics = IOSNotificationDetails();
    // var platformChannelSpecifics = NotificationDetails(
    //   android: androidChannelSpecifics,
    //   iOS: iOSChannelSpecifics,
    // );
    // await flutterLocalNotificationsPlugin.show(
    //   notification.id,
    //   notification.title,
    //   notification.body,
    //   platformChannelSpecifics,
    // );
  }
}

注意事项

  1. API Key:确保替换YOUR_NOTIFICARE_API_KEY为你的实际Notificare API Key。
  2. 环境:根据你的开发环境,选择productiondevelopment
  3. 本地通知:上面的showLocalNotification方法里注释掉了显示本地通知的代码。你可以使用flutter_local_notifications或其他本地通知插件来显示通知。
  4. 错误处理:在初始化过程中添加错误处理,以便在初始化失败时能够捕获并处理错误。

确保你按照notificare_push插件的官方文档完成了所有必要的配置和步骤,因为插件的具体使用方式可能会随着版本的更新而有所变化。

回到顶部