JPush 极光推送扩展
集成极光推送(JPush)服务,让 App Inventor 2 应用轻松接收远程推送通知。
核心特性:
- 3 步完成接入:设置 AppKey → Initialize → 监听事件
- 支持通知消息和自定义消息(透传)
- 别名和标签管理,精准推送
- 推送时间段和静默时间段控制
-
通知权限检查和引导
- .aix 拓展下载:
快速开始
3 步接入推送
第 1 步:在设计器中设置极光 AppKey(从 极光后台 获取)
第 2 步:初始化
when Screen1.Initialize() {
JPush1.Initialize()
}
第 3 步:监听推送事件
when JPush1.OnNotificationOpened(title, content, extras) {
标签1.Text = content
}
when JPush1.OnMessageReceived(message, extras) {
// 处理自定义消息(透传消息,不显示通知栏)
标签2.Text = message
}
获取设备注册 ID
when JPush1.OnConnected(registrationId) {
标签_注册ID.Text = registrationId
// 将此 ID 发送给你的服务器,用于定向推送
}
方法
初始化
- Initialize()
- 初始化极光推送。在设计器中设置 AppKey 后调用此方法即可开始接收推送
推送控制
- StopPush()
- 停止推送服务,停止后将不再接收推送消息
- ResumePush()
- 恢复推送服务
- IsPushStopped()
- 查询推送服务是否已停止
设备信息
- GetRegistrationID()
- 获取设备在极光的唯一注册 ID
别名和标签
- SetAlias(alias)
- 设置设备别名。每个设备只能设置一个别名,通常用作用户标识(如用户ID)
- DeleteAlias()
- 删除已设置的别名
- SetTags(tags)
- 设置设备标签,用于分组推送。会覆盖之前设置的标签
- SetAliasAndTags(alias, tags)
- 同时设置别名和标签
时间控制
- SetPushTime(startHour, endHour, days)
- 设置允许推送的时间段。startHour/endHour: 0-23; days: 允许推送的星期(1=周日, 2=周一, …, 7=周六),如 [2,3,4,5,6] 表示工作日
- SetSilenceTime(startHour, startMinute, endHour, endMinute)
- 设置静默时间段,此时间段内不会弹出通知
通知管理
- ClearAllNotifications()
- 清除本应用所有极光推送的通知
- ClearNotificationById(notificationId)
- 清除指定 ID 的通知
- AreNotificationEnabled()
- 检查当前应用是否允许显示通知
- RequestNotificationPermission()
- 打开系统通知设置页面,引导用户开启通知权限
事件
消息事件
- OnMessageReceived(message, extras)
- 收到自定义消息(透传消息,不显示通知栏)。message: 消息内容; extras: 附加信息(JSON格式)
- OnNotificationReceived(title, content, extras)
- 收到通知消息(应用在前台时触发)。title: 通知标题; content: 通知内容; extras: 附加信息
- OnNotificationOpened(title, content, extras)
- 用户点击了通知栏消息。title: 通知标题; content: 通知内容; extras: 附加信息
连接事件
- OnConnected(registrationId)
- 成功连接到极光服务器。registrationId: 设备注册 ID
操作结果
- OnAliasSet(code, alias)
- 别名设置/删除的结果。code 为 0 表示成功
- OnTagSet(code, tags)
- 标签设置的结果。code 为 0 表示成功
属性
- AppKey
- 极光推送 AppKey,在 极光后台 应用设置中获取
- Channel
- 推送渠道名称,用于统计分发渠道,默认 developer-default
- DebugMode
- 调试模式,开启后输出详细日志到 Logcat,默认 false
使用场景示例
场景1:接收通知并跳转
当 JPush1.OnNotificationOpened(title, content, extras) 时:
设置 全局变量 lastTitle 为 title
设置 全局变量 lastContent 为 content
调用 另一个屏幕.OpenScreen() // 跳转到详情页
场景2:用户登录后绑定别名
when 按钮_登录.Click() {
// ... 登录成功后
JPush1.SetAlias("user_" + userId)
}
when JPush1.OnAliasSet(code, alias) {
if code == 0 {
标签_状态.Text = "推送绑定成功"
} else {
标签_状态.Text = "推送绑定失败"
}
}
场景3:按标签分组推送
当 Screen1.Initialize 时:
调用 JPush1.Initialize()
// 标记为 VIP 用户
调用 JPush1.SetTags([创建列表]("vip", "premium"))
当 JPush1.OnTagSet(code, tags) 时:
如果 code = 0 那么:
设置 LabelTag.Text 为 "标签设置成功: " + tags
场景4:只在工作日推送
当 Screen1.Initialize 时:
调用 JPush1.Initialize()
// 周一到周五 (2-6),9:00-22:00 接收推送
调用 JPush1.SetPushTime(9, 22, [创建列表](2, 3, 4, 5, 6))
场景5:夜间静默
when Screen1.Initialize() {
JPush1.Initialize()
// 22:00 - 次日 8:00 不弹通知
JPush1.SetSilenceTime(22, 0, 8, 0)
}
注意事项
- AppKey 必须在设计器中设置,不能留空
- Initialize() 需要在 Screen.Initialize 中调用,不能延迟调用
- 通知权限:Android 13+ 需要
POST_NOTIFICATIONS权限,扩展已声明但用户仍需授权 - extras 参数为 JSON 格式字符串,可用 Web.JsonTextDecode 解析
- 别名设置有频率限制(极光限制 10秒内最多 3 次)
- 标签设置有频率限制(极光限制 10秒内最多 3 次)
版本信息
- 当前版本:1
- JPush SDK:5.6.0
- 兼容性:App Inventor 2(仅 Android)
扫码添加客服咨询