Flutter 3.0 腾讯推送

该博客详细介绍了如何在Flutter应用中集成TPNS(腾讯信鸽)推送服务,包括添加Flutter TPNS插件、配置AndroidManifest.xml、初始化代码及设置各种事件回调。此外,还提供了官方文档链接和示例代码,帮助开发者实现推送消息的接收和处理。
摘要由CSDN通过智能技术生成

官网文档

https://github.com/TencentCloud/TPNS-Flutter-Plugin
  1. 添加插件:
 $ flutter tpns_flutter_plugin 
  1. 配置 android\app\build.gradle
      android: {
         ....
         defaultConfig {
           applicationId "替换成自己应用 ID" //申请的时包名
           ...
           ndk {
        /// 选择要添加的对应.so 库。
        abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a',
           }
           // 注意是 +号
           manifestPlaceholders += [
               XG_ACCESS_ID : "替换自己的ACCESS_ID",  // 信鸽官网注册所得ACCESS_ID
               XG_ACCESS_KEY : "替换自己的ACCESS_KEY",  // 信鸽官网注册所得ACCESS_KEY
    
           ]
         }
       }

  1. 代码初始化
import 'package:flutter/material.dart';
import 'package:tpns_flutter_plugin/tpns_flutter_plugin.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);
  
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final XgFlutterPlugin tpush = XgFlutterPlugin();

  
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    if (!mounted) return;

    /// 开启DEBUG
    tpush.setEnableDebug(true);

    /// 添加回调事件
    tpush.addEventHandler(
      onRegisteredDeviceToken: (String msg) async {
        print("flutter onRegisteredDeviceToken: $msg");
        print("=====1==============");
      },
      onRegisteredDone: (String msg) async {
        print("flutter onRegisteredDone: $msg");
        print("注册成功");
        print("=====2==============");
      },
      unRegistered: (String msg) async {
        print("flutter unRegistered: $msg");
        print(msg);
        print("=====3==============");
      },
      /// 主要事件:  接收到的推送事件
      onReceiveNotificationResponse: (Map<String, dynamic> msg) async {
        print("flutter onReceiveNotificationResponse $msg");
        print("=====4==============");
      },
      onReceiveMessage: (Map<String, dynamic> msg) async {
        print("flutter onReceiveMessage $msg");
        print("=====5==============");
      },
      xgPushDidSetBadge: (String msg) async {
        print("flutter xgPushDidSetBadge: $msg");
        print("=====6==============");

        /// 在此可设置应用角标
        /// tpush.setAppBadge(0);
        print(msg);
      },
      xgPushDidBindWithIdentifier: (String msg) async {
        print("flutter xgPushDidBindWithIdentifier: $msg");
        print(msg);
        print("=====7==============");
      },
      xgPushDidUnbindWithIdentifier: (String msg) async {
        print("flutter xgPushDidUnbindWithIdentifier: $msg");
        print(msg);
        print("=====8==============");
      },
      xgPushDidUpdatedBindedIdentifier: (String msg) async {
        print("flutter xgPushDidUpdatedBindedIdentifier: $msg");
        print(msg);
        print("=====9==============");
      },
      xgPushDidClearAllIdentifiers: (String msg) async {
        print("flutter xgPushDidClearAllIdentifiers: $msg");
        print(msg);
        print("=====10==============");
      },
      /// 主要事件:  点击通知的触发的事件
      xgPushClickAction: (Map<String, dynamic> msg) async {
        print("flutter xgPushClickAction $msg");
        print("clickResponse");
        print("=====11==============");
        print(msg["title"] + "," + msg["content"]); //推送的标题,内容.
        print("点击了推送,在这里可以做页面跳转判断");
        print("=====11==============");
      },
    );
	
	///  建议申请的时候选择广州, 这样就不要额外设置了, 默认就是广州集群.

    /// 如果您的应用非广州集群则需要在startXG之前调用此函数
    /// 香港:tpns.hk.tencent.com
    /// 新加坡:tpns.sgp.tencent.com
    /// 上海:tpns.sh.tencent.com
    /// tpush.configureClusterDomainName("tpns.sh.tencent.com");

    /// 启动TPNS服务
    /// 注意:为了不影响回调效果,建议在应用启动方法优先添加回调以及调用TPNS注册方法
    tpush.startXg("xxx", "xxx");
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(),
    );
  }
}

flutter内置 图标网:

https://www.fluttericon.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值