capacitor-jpush-core
v0.0.4
Published
极光推送Capacitor插件,支持Android和iOS平台基本推送功能
Downloads
363
Maintainers
Readme
capacitor-jpush-core
极光推送Capacitor插件,支持Android和iOS平台的一些简单功能。
Install
npm install capacitor-jpush-core
npx cap sync平台支持
| 功能 | iOS | Android | 备注 | |------|-----|---------|------| | 设置别名 | ✅ | ✅ | | | 删除别名 | ✅ | ✅ | | | 获取注册ID | ✅ | ✅ | | | 设置角标 | ✅ | ❌ | 未集成厂商通道 | | 通知接收 | ✅ | ✅ | | | 通知点击回调 | ✅ | ✅ | Android仅限App运行时 | | 自定义消息 | ✅ | ✅ | | | 权限检查 | ✅ | ✅ | | | 权限请求 | ✅ | ✅ | |
Capacitor.config 配置
{
"plugins": {
"JPush": {
// 极光推送AppKey
"appKey": "your_app_key",
// 推送通道,默认值为default, 没统计需求随便填
"channel": "default",
// 是否为生产环境,默认值为false
"production": false,
// 进入App时是否自动清除角标,默认值为false
"activeClearBadge": false,
// 是否跟随App启动时自动注册极光推送,默认值为false
"iosAutoRegister": true
}
}
}iOS 准备工作
xcode 配置
- 在 sign & capabilities 中点击
+ capability按钮,搜索并添加Background Modes和Push Notifications选项。 - 在
Background Modes中勾选Remote notifications子项。
AppDelegate 基础配置
在主工程的AppDelegate以下代理方法中,添加发送通知的代码
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidBecomeActive(_ application: UIApplication) {
// 从后台到前台,包括冷启动都会进入这个方法
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 通知设备注册成功的消息
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// 通知设备注册失败的消息
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}
}推送唤醒
- 当应用在后台收到远程推送时,会触发
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)方法。 如果需要增加该消息处理,需要在AppDelegate中添加以下代码和通知扩展,插件就能把该推送传给web层的接收消息监听JPushEventName.NotificationReceived。
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationCenter.default.post(name: .didReceiveRemotePush, object: userInfo)
completionHandler(.newData)
}
}
// 注意: 包括这个事件的定义也要复制进去
extension NSNotification.Name {
static let didReceiveRemotePush = NSNotification.Name("CapacitorJPushCoreBackgroundPush")
}注意:后端发的推送需要配置推送唤醒
未运行时点击推送消息
如果需要处理回调的话,Capacitor.config 配置文件需要设置iosAutoRegister为true。必须在启动时就注册极光推送,否则本次点击推送消息不会触发后续在应用中添加的监听。
{
"plugins": {
"JPush": {
"iosAutoRegister": true
}
}
}Android 准备工作
我们使用capacitor配置,但是这里JPush需要一些元数据的占位符。 在 Android 项目的 app/build.gradle 文件中,找到 android.defaultConfig 或对应 productFlavor 块,添加以下 manifestPlaceholders 配置:
defaultConfig {
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY: "",
JPUSH_CHANNEL: "default"
]
}API
setupJPush()getRegistrationID()setAlias(...)deleteAlias(...)setTags(...)addTags(...)deleteTags(...)cleanTags()checkPermissions()requestPermissions()setBadge(...)addListener(JPushEventName.NotificationReceived, ...)addListener(JPushEventName.NotificationOpened, ...)addListener(JPushEventName.CustomMessageReceived, ...)addListener(JPushEventName.RegistrationCompleted, ...)addListener(JPushEventName.RegistrationFailed, ...)- Interfaces
- Type Aliases
- Enums
setupJPush()
setupJPush() => Promise<void>getRegistrationID()
getRegistrationID() => Promise<RegistrationIDResult>Returns: Promise<RegistrationIDResult>
setAlias(...)
setAlias(options: AliasOptions) => Promise<void>| Param | Type |
| ------------- | ----------------------------------------------------- |
| options | AliasOptions |
deleteAlias(...)
deleteAlias(options?: DeleteAlias | undefined) => Promise<void>| Param | Type |
| ------------- | --------------------------------------------------- |
| options | DeleteAlias |
setTags(...)
setTags(options: SetTagsOptions) => Promise<void>| Param | Type |
| ------------- | --------------------------------------------------------- |
| options | SetTagsOptions |
addTags(...)
addTags(options: SetTagsOptions) => Promise<void>| Param | Type |
| ------------- | --------------------------------------------------------- |
| options | SetTagsOptions |
deleteTags(...)
deleteTags(options: SetTagsOptions) => Promise<void>| Param | Type |
| ------------- | --------------------------------------------------------- |
| options | SetTagsOptions |
cleanTags()
cleanTags() => Promise<void>checkPermissions()
checkPermissions() => Promise<PermissionStatus>Returns: Promise<PermissionStatus>
requestPermissions()
requestPermissions() => Promise<PermissionStatus>Returns: Promise<PermissionStatus>
setBadge(...)
setBadge(options: SetBadgeOptions) => Promise<void>| Param | Type |
| ------------- | ----------------------------------------------------------- |
| options | SetBadgeOptions |
addListener(JPushEventName.NotificationReceived, ...)
addListener(eventName: JPushEventName.NotificationReceived, listener: (data: PushNotificationData) => void) => Promise<PluginListenerHandle>| Param | Type |
| --------------- | ---------------------------------------------------------------------------------------- |
| eventName | JPushEventName.NotificationReceived |
| listener | (data: PushNotificationData) => void |
Returns: Promise<PluginListenerHandle>
addListener(JPushEventName.NotificationOpened, ...)
addListener(eventName: JPushEventName.NotificationOpened, listener: (data: PushNotificationData) => void) => Promise<PluginListenerHandle>| Param | Type |
| --------------- | ---------------------------------------------------------------------------------------- |
| eventName | JPushEventName.NotificationOpened |
| listener | (data: PushNotificationData) => void |
Returns: Promise<PluginListenerHandle>
addListener(JPushEventName.CustomMessageReceived, ...)
addListener(eventName: JPushEventName.CustomMessageReceived, listener: (data: CustomMessageData) => void) => Promise<PluginListenerHandle>| Param | Type |
| --------------- | ---------------------------------------------------------------------------------- |
| eventName | JPushEventName.CustomMessageReceived |
| listener | (data: CustomMessageData) => void |
Returns: Promise<PluginListenerHandle>
addListener(JPushEventName.RegistrationCompleted, ...)
addListener(eventName: JPushEventName.RegistrationCompleted, listener: (data: RegistrationCompletedData) => void) => Promise<PluginListenerHandle>| Param | Type |
| --------------- | -------------------------------------------------------------------------------------------------- |
| eventName | JPushEventName.RegistrationCompleted |
| listener | (data: RegistrationCompletedData) => void |
Returns: Promise<PluginListenerHandle>
addListener(JPushEventName.RegistrationFailed, ...)
addListener(eventName: JPushEventName.RegistrationFailed, listener: (data: RegistrationFailedData) => void) => Promise<PluginListenerHandle>| Param | Type |
| --------------- | -------------------------------------------------------------------------------------------- |
| eventName | JPushEventName.RegistrationFailed |
| listener | (data: RegistrationFailedData) => void |
Returns: Promise<PluginListenerHandle>
Interfaces
RegistrationIDResult
| Prop | Type |
| -------------------- | ------------------- |
| registrationID | string |
AliasOptions
| Prop | Type |
| ----------- | ------------------- |
| alias | string |
| seq | number |
DeleteAlias
| Prop | Type |
| --------- | ------------------- |
| seq | number |
SetTagsOptions
| Prop | Type |
| ---------- | --------------------- |
| tags | string[] |
| seq | number |
PermissionStatus
权限状态接口
| Prop | Type | Description |
| ---------------- | ------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| permission | 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied' | 权限状态 - prompt: 首次申请,询问 - prompt-with-rationale:每次都询问 - granted:已获取权限 - denied:权限已拒绝 |
SetBadgeOptions
| Prop | Type |
| ----------- | ------------------- |
| badge | number |
PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove | () => Promise<void> |
PushNotificationData
| Prop | Type |
| -------------------- | ------------------------------------------------------------ |
| title | string |
| body | string |
| subtitle | string |
| badge | number |
| sound | string |
| extras | Record<string, any> |
| messageId | string |
| notificationId | number |
| type | 'notification' | 'custom' | 'silent' |
| rawData | Record<string, any> |
CustomMessageData
| Prop | Type |
| --------------- | ------------------------------------------------------------ |
| title | string |
| body | string |
| extras | Record<string, any> |
| messageId | string |
| type | 'notification' | 'custom' | 'silent' |
RegistrationCompletedData
| Prop | Type |
| -------------------- | ------------------- |
| registrationID | string |
RegistrationFailedData
| Prop | Type |
| ------------------ | ------------------- |
| errorCode | number |
| errorMessage | string |
Type Aliases
Record
Construct a type with a set of properties K of type T
{ [P in K]: T; }
Enums
JPushEventName
| Members | Value | Description |
| --------------------------- | ------------------------------------ | ----------- |
| NotificationReceived | 'notificationReceived' | 通知接收事件 |
| NotificationOpened | 'notificationOpened' | 通知点击事件 |
| CustomMessageReceived | 'customMessageReceived' | 自定义消息接收事件 |
| RegistrationCompleted | 'registrationCompleted' | 注册完成事件 |
| RegistrationFailed | 'registrationFailed' | 注册失败事件 |
开发依赖
- Capacitor 7 配套设施
- Node.js 20+
- TypeScript: ^5.9.3
各平台要求,参考capacitor7文档
iOS 14+
xcode 16+
swift 5.9+
Android 6.0+
android studio 2024.2.1+
Java JDK 21
gradle plugin to 8.7.2
gradle wrapper to 8.11.1
kotlin 1.9.25
