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

您所在的位置:网站首页 华为推送sdk消息 【Flutter】极光推送配置流程(极光通道/华为厂商/IOS) 章一

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

2024-07-17 13:12:12| 来源: 网络整理| 查看: 265

极光推送配置流程

相关文章

推送配置共三篇(如下链接) 【Flutter】极光推送配置流程(极光通道/华为厂商/IOS) 章一 【Flutter】极光推送配置流程(小米厂商通道) 章二 【Flutter】极光推送配置流程(VIVO/OPPO/荣耀厂商通道) 章三

极光通道(在线)

配置时间 2024年3月11日

创建应用

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

创建Flutter项目 在这里插入图片描述 填写包名 在这里插入图片描述

创建完成 在这里插入图片描述

项目配置

运行命令行 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 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. @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Flutter 极光推送', home: HomePage(), ); } } class HomePage extends StatefulWidget { @override State createState() => _HomePageState(); } class _HomePageState extends State { @override void initState() { JPushUtil().initPlatformState(); super.initState(); } @override Widget build(BuildContext context) { return const Scaffold( body: Center( child: Text("极光推送配置"), ), ); } } 运行项目

运行项目在Android端,有getRegistrationID,则注册成功 在这里插入图片描述

测试接收推送之前,先打开接收通知的权限 在这里插入图片描述

在极光平台检测集成 在这里插入图片描述

推送消息 平台推送

直接在平台客户端输入对应的数值来推送消息 在这里插入图片描述 在这里插入图片描述 注:当APP被杀死后,极光通道的推送就不能及时收到了,所以要配置厂商通道

推送API

通过接口调用推送

厂商通道(华为) 创建项目 创建应用

应用服务 - PUSH 在这里插入图片描述 在这里插入图片描述 继续 在这里插入图片描述 选择中国 在这里插入图片描述 开通推送服务 在这里插入图片描述 在这里插入图片描述 添加应用 在这里插入图片描述 输入相关信息 在这里插入图片描述 下载agconnect-services.json 在这里插入图片描述 放到应用级目录下 在这里插入图片描述

SHA256证书指纹

在这里插入图片描述

生成签名jks

生成签名文件的参考文档链接 在这里插入图片描述 创建新的Key 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

生成签名证书指纹

** keytool -list -v -keystore ./demostration_project_key.jks** 在这里插入图片描述

配置SHA256证书指纹

在这里插入图片描述

项目配置

若出现以下报错 在这里插入图片描述 修改Manifest.xml文件 在这里插入图片描述

修改android/build.gradle文件 在这里插入图片描述

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/' } } }

若出现以下报错 在这里插入图片描述 那说明漏了配置AGC插件,参考相关链接 在这里插入图片描述 接着添加依赖 在这里插入图片描述

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 '123456' //keystore的文件路径,可以是绝对路径也可以是相对路径 storeFile file('../demostration_project_key.jks') //keystore的密码l storePassword '123456' } }

最后在极光平台厂商通道配置设置App ID 和App Secret,并启用 在这里插入图片描述

华为平台配置

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

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

若出现以下这句,就代表配置成功了 在这里插入图片描述 这里的检测会有延迟,真机测试能离线收到消息就ok 在这里插入图片描述

错误集锦 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、权限

网络权限记得加上

Ios

官方配置参考链接

Push

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

Podfile

在这里插入图片描述

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

终端 pod install 在这里插入图片描述

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}); @override 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; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { final String appKey = "XXX"; final String masterSecret = "XXX"; late String base64AuthString; final Dio dio = Dio(); late String notificationAlert; late String notificationTitle; late String notificationAudienceAlias; @override void initState() { final content = utf8.encode("$appKey:$masterSecret"); base64AuthString = "Basic ${base64Encode(content)}"; super.initState(); } @override 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 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()); } }


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭