【Flutter】极光推送配置流程(极光通道/华为厂商/IOS) 章一

【Flutter】极光推送配置流程(极光通道/华为厂商/IOS) 章一极光推送 配置极光通道 华为通道 IosAPNs 极光推送

大家好,欢迎来到IT知识分享网。

极光推送配置流程

相关文章

极光通道(在线)

配置时间 2024年3月11日

创建应用

在这里插入图片描述
应用列表 – 创建应用
在这里插入图片描述
Android – 选中消息推送 – 下一步


项目配置

运行命令行 flutter pub add jpush_flutter
在这里插入图片描述

Android -> app -> build.gradle 更改JPUSH_APPKEY的值
在这里插入图片描述
在这里插入图片描述

工具类注册JPush Android

import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; class JPushUtil { 
    JPushUtil._internal(); static final _instance = JPushUtil._internal(); factory JPushUtil() => _instance; final JPush jPush = JPush(); Future<void> initPlatformState() async { 
    String? platformVersion; try { 
    jPush.addEventHandler( onReceiveNotification: (message) async { 
    print("flutter onReceiveNotification: $message"); }, onOpenNotification: (message) async { 
    print("flutter onOpenNotification: $message"); }, onReceiveMessage: (message) async { 
    print("flutter onReceiveMessage: $message"); }, onReceiveNotificationAuthorization: (message) async { 
    print("flutter onReceiveNotificationAuthorization: $message"); }, onConnected: (message) { 
    print("flutter onConnected: $message"); return Future(() => null); }, ); } on PlatformException { 
    platformVersion = 'Failed to get platform version.'; print(platformVersion); } jPush.isNotificationEnabled().then((bool value) { 
    print("通知授权是否打开: $value"); if (!value) { 
    Get.snackbar( "提示", "没有通知权限,点击跳转打开通知设置界面", duration: const Duration(seconds: 6), onTap: (_) { 
    jPush.openSettingsForNotification(); }, ); } }).catchError((onError) { 
    print("通知授权是否打开: ${ 
     onError.toString()}"); }); jPush.enableAutoWakeup(enable: true); jPush.setup( appKey: '7f684a39ff1f95ef1657acdd', production: true, debug: true, ); jPush.applyPushAuthority( const NotificationSettingsIOS( sound: true, alert: true, badge: true, ), ); final rid = await jPush.getRegistrationID(); print("flutter getRegistrationID: $rid"); } setAlias(String aliasStr) { 
    final alias = jPush.setAlias(aliasStr); print("Alias is $alias"); } } 

main.dart

import 'package:demonstration_project/jPushUtil.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; void main() { 
    runApp(const MyApp()); } class MyApp extends StatelessWidget { 
    const MyApp({ 
   super.key}); // This widget is the root of your application.  Widget build(BuildContext context) { 
    return GetMaterialApp( title: 'Flutter 极光推送', home: HomePage(), ); } } class HomePage extends StatefulWidget { 
     State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { 
     void initState() { 
    JPushUtil().initPlatformState(); super.initState(); }  Widget build(BuildContext context) { 
    return const Scaffold( body: Center( child: Text("极光推送配置"), ), ); } } 

运行项目

推送消息

平台推送

推送API

通过接口调用推送

厂商通道(华为)

创建项目 创建应用

SHA256证书指纹

在这里插入图片描述

生成签名jks

生成签名证书指纹

配置SHA256证书指纹

在这里插入图片描述

项目配置

buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() // 配置HMS Core SDK的Maven仓地址。 maven { url 'https://developer.huawei.com/repo/' } } dependencies { classpath 'com.android.tools.build:gradle:7.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.huawei.agconnect:agcp:1.6.0.300' } } allprojects { repositories { google() mavenCentral() maven { url 'https://developer.huawei.com/repo/' } } } 
dependencies { // 极光推送 'cn.jiguang.sdk:jpush:5.2.3' 和 'cn.jiguang.sdk.plugin:huawei:5.2.3' 版本需一致 implementation 'cn.jiguang.sdk:jpush:5.2.3' implementation 'com.huawei.agconnect:agconnect-core:1.8.1.300' implementation 'com.huawei.hms:push:6.12.0.300' implementation 'cn.jiguang.sdk.plugin:huawei:5.2.3' // 这里版本一定要和极光插件版本一致!!! } 
 signingConfigs { debug { //keystore中key的别名 keyAlias 'demo_key' //keystore中key的密码 keyPassword '' //keystore的文件路径,可以是绝对路径也可以是相对路径 storeFile file('../demostration_project_key.jks') //keystore的密码l storePassword '' } } 

华为平台配置

在这里插入图片描述
回执配置参考链接
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里的自分类是华为控制滥发消息的,需要设置一下,参考链接
在这里插入图片描述
选一个要发的场景
在这里插入图片描述
填写信息,和场景相关的信息
在这里插入图片描述
在这里插入图片描述
激活功能
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述














找一台华为手机来运行项目

错误集锦

1、版本不一致

1、极光厂商插件版本和JPush版本不一致,即便输出huawei token,也是收不到离线消息的

 // 极光推送 'cn.jiguang.sdk:jpush:5.2.3' 和 'cn.jiguang.sdk.plugin:huawei:5.2.3' 版本需一致 implementation 'cn.jiguang.sdk:jpush:5.2.3' // 华为 implementation 'com.huawei.agconnect:agconnect-core:1.8.1.300' implementation 'com.huawei.hms:push:6.12.0.300' implementation 'cn.jiguang.sdk.plugin:huawei:5.2.3' 

2、权限

网络权限记得加上

<uses-permission android:name="android.permission.INTERNET" /> 

Ios

官方配置参考链接

Push

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Podfile

在这里插入图片描述

 pod 'JPush' pod 'JOperate' pod 'JCore' 

Token Authentication配置

在这里插入图片描述
developer.apple.com
在这里插入图片描述
后续步骤直接参考上面的链接,很详细
最后获取到的
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述






运行项目

在这里插入图片描述
配置完成

推送API

推送API参考文档

鉴权方式

用冒号拼接appkey和masterSecret,并用base64加密该字符串,最终再拼上”Basic “

 final content = utf8.encode("$appKey:$masterSecret"); String base64AuthString = "Basic ${ 
     base64Encode(content)}"; 

测试代码

import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; void main() { 
    runApp(const MyApp()); } class MyApp extends StatelessWidget { 
    const MyApp({ 
   super.key});  Widget build(BuildContext context) { 
    return MaterialApp( title: '推送', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: '信息推送'), ); } } class MyHomePage extends StatefulWidget { 
    const MyHomePage({ 
   super.key, required this.title}); final String title;  State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { 
    final String appKey = "XXX"; final String masterSecret = "XXX"; late String base64AuthString; final Dio dio = Dio(); late String notificationAlert; late String notificationTitle; late String notificationAudienceAlias;  void initState() { 
    final content = utf8.encode("$appKey:$masterSecret"); base64AuthString = "Basic ${ 
     base64Encode(content)}"; super.initState(); }  Widget build(BuildContext context) { 
    return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Padding( padding: const EdgeInsets.all(8.0), child: ListView( children: [ TextField( decoration: const InputDecoration( labelText: "主标题", hintText: "请输入...", ), onChanged: (s) { 
    notificationAlert = s; }, ), TextField( decoration: const InputDecoration( labelText: "副标题", hintText: "请输入...", ), onChanged: (s) { 
    notificationTitle = s; }, ), TextField( decoration: const InputDecoration( labelText: "别名", hintText: "请输入...", ), onChanged: (s) { 
    notificationAudienceAlias = s; }, ), Padding( padding: const EdgeInsets.all(8.0), child: ElevatedButton( onPressed: () { 
    showDialog( context: context, builder: (context) { 
    return SimpleDialog( title: const Text("确定发送?"), children: [ SimpleDialogOption( child: const Text("确定"), onPressed: () { 
    pushMessage( notificationAlert: notificationAlert, notificationTitle: notificationTitle, notificationAudienceAlias: [ notificationAudienceAlias ], ); Navigator.of(context).pop(); }, ), SimpleDialogOption( child: const Text("取消"), onPressed: () { 
    Navigator.of(context).pop(); }, ) ], ); }); }, child: const Text("推送消息"), ), ), ], ), ), ), ); } /// 推送 pushMessage({ 
    required String notificationAlert, required String notificationTitle, required List<String> notificationAudienceAlias, }) async { 
    const String url = "https://api.jpush.cn/v3/push"; var data = json.encode({ 
    "platform": ["android", "ios"], "inapp_message": { 
   "inapp_message": false}, "options": { 
    "classification": 0, "time_to_live": 86400, "apns_production": false, "third_party_channel": { 
    "huawei": { 
    "skip_quota": false, "distribution": "secondary_push", "channel_id": "", "category": "DEVICE_REMINDER", "receipt_id": "" } } }, "notification": { 
    "alert": notificationAlert, "android": { 
    "alert": notificationAlert, "title": notificationTitle, "intent": { 
   "url": "intent:#Intent;action=android.intent.action.MAIN;end"}, "sound": "", "priority": 0, "category": "", "alert_type": 7, "style": 0, "builder_id": 0, "large_icon": "", "badge_add_num": 1, "extras": { 
    "param": "123" } }, "ios": { 
    "alert": { 
    "title": notificationAlert, "body": notificationTitle, }, "content-available": 0, "mutable-content": 1, "sound": "default", "badge": "+1", "thread-id": "", "interruption-level": "active", "filter-criteria": "", "extras": { 
    "参数": "A" } } }, "audience": { 
    "alias": notificationAudienceAlias, } }); final response = await dio.request( url, data: data, options: Options( headers: { 
    HttpHeaders.authorizationHeader: base64AuthString, }, method: "POST", ), ); print(response.data.toString()); } } 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/128817.html

(0)
上一篇 2025-08-28 20:33
下一篇 2025-08-28 21:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信