极光推送Flutter插件-集成厂商推送

这篇教程详细介绍了如何在Flutter项目中集成华为和小米的厂商推送,以Jcenter为示例。步骤包括在android/build.gradle添加依赖,放置json和签名文件,修改appkey和包名,更新build.gradle文件,以及根据错误提示在AndroidManifest.xml中添加必要代码。

此教程以集成华为和小米为例(Jcenter集成),华为比较特殊,也是客户问得最多的厂商,其它厂商集成基本就按照小米集成的样子照葫芦画瓢即可

(1)在工程 jpush-flutter-plugin-demo/android/build.gradle加入依赖

 //华为
    implementation 'com.huawei.hms:push:5.0.0.300'
    implementation 'cn.jiguang.sdk.plugin:huawei:3.8.0'
    //小米
    implementation 'cn.jiguang.sdk.plugin:xiaomi:3.8.0'

如图所示:

image.png

(2)在工程 jpush-flutter-plugin-demo/example/android/app/build.gradle加入代码

apply plugin: 'com.huawei.agconnect'

把从华为官网下载的json文件和自己的签名文件拷贝进去工程如图所示目录

image.png

换上自己的包名和相关appkey等参数

image.png

应用上自己的签名

Flutter 项目中集成 JPush极光推送)是实现跨平台消息推送的重要步骤,涉及 Android 和 iOS 平台的配置。以下是完整的集成指南。 ### ### 添加依赖 首先,在 `pubspec.yaml` 文件中添加极光推送Flutter 插件: ```yaml dependencies: jpush_flutter: ^0.7.3 ``` 执行 `flutter pub get` 安装插件。 ### ### 初始化 SDK 在程序入口文件(例如 `main.dart`)中引入插件并初始化: ```dart import 'package:flutter/material.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { final JPush jpush = JPush(); @override void initState() { super.initState(); jpush.setup( appKey: "你的 AppKey", channel: "theChannel", production: false, debug: true, ); jpush.applyPushAuthority(new NotificationSettingsIOS(sound: true, alert: true, badge: true)); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('JPush 集成示例')), body: Center(child: Text('欢迎使用极光推送!')), ), ); } } ``` ### ### Android 配置 1. 在 `android/app/src/main/AndroidManifest.xml` 中确保包含以下权限声明[^2]: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> ``` 2. 在 `android/build.gradle` 中确认仓库地址为: ```gradle repositories { google() mavenCentral() } ``` ### ### iOS 配置 1. 使用 Xcode 打开 iOS 项目,并启用推送功能:`TARGETS -> Capabilities -> Push Notifications` 设为 ON 状态[^2]。 2. 在 `ios/Runner/AppDelegate.swift` 或 `AppDelegate.m` 中添加以下代码以支持推送: ```swift import jpush_flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { JPUSHService.registerForRemoteNotifications() return super.application(application, didFinishLaunchingWithOptions: launchOptions) } } ``` ### ### 处理推送消息 可以监听通知事件来处理前台收到的消息: ```dart jpush.addEventHandler(events: [ JPushEvent.notificationReceived, JPushEvent.notificationOpened, ], callback: (Map<String, dynamic> event) { print("接收到推送消息: $event"); }); ``` ###
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值