capacitor-plugin-jpushn
v6.0.7
Published
JPush New Version
Downloads
516
Readme
capacitor-plugin-jpushn
JPush New Version
大版本跟@capacitor
ref: capacitor-plugin-jpush
v6.x support
Capacitor 6
Install
npm install capacitor-plugin-jpushn
npx cap sync
Usage
in capacitor.config.ts
:
/// <reference types="capacitor-plugin-jpush" />
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
JPush: {
// your application appKey on JPush
appKey: '',
channel: '',
},
},
};
export default config;
in capacitor.config.json
:
{
"plugins": {
"JPush": {
"appKey": "",
"channel": ""
}
}
}
IOS
On iOS you must enable the Push Notifications capability. See Setting Capabilities for instructions on how to enable the capability.
After enabling the Push Notifications capability, add the following to your app's AppDelegate.swift
:
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)
}
// add the following code to applicationDidBecomeActive function
NotificationCenter.default.post(name: Notification.Name(rawValue: "didBecomeActiveNotification"), object: nil)
then use Xcode to open your native project, and set JPUSHService.h
file's Target MemberShip
to CapacitorPluginJPush
which value is Public
:
Android
Android 13 requires a permission check in order to send notifications. You are required to call checkPermissions()
and requestPermissions()
accordingly.
On Android 12 and older it won't show a prompt and will just return as granted.
please set both compileSdkVersion
and targetSdkVersion
to 33
in variables.gradle
:
android studio
add the following to your app's build.gradle
:
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
]
Currently does not support the manufacturer channel push
Example
import { Capacitor } from '@capacitor/core';
import { JPush } from 'capacitor-plugin-jpush';
const jpushSetup = async () => {
if (Capacitor.isNativePlatform()) {
// addListener events
const receivedEvent = await JPush.addListener(
'notificationReceived',
data => {
console.log(data);
},
);
// if you don't need,you can remove
receivedEvent.remove();
JPush.addListener('notificationOpened', data => {
console.log(data);
});
JPush.checkPermissions().then(({ notifications }) => {
console.log(notifications);
if (notifications === 'prompt' || notifications === 'denied') {
// apply notification permission
JPush.requestPermissions().then(res => {
console.log(res.notifications);
});
}
});
}
};
const jpushMethods = async () => {
// set alias
await JPush.setAlias({
alias: 'alias',
});
// getRegistrationID
const { registrationId } = await JPush.getRegistrationID();
console.log(registrationId);
// ......
};
API
startJPush()
setDebugMode(...)
setAlias(...)
deleteAlias(...)
addTags(...)
deleteTags(...)
cleanTags()
setBadgeNumber(...)
removeListeners()
getRegistrationID()
checkPermissions()
requestPermissions()
openNotificationSetting()
addListener('notificationReceived', ...)
addListener('notificationOpened', ...)
- Interfaces
- Type Aliases
startJPush()
startJPush() => any
启动极光推送服务,即使没有获取到通知权限,也会进行推送服务初始化
Returns: any
setDebugMode(...)
setDebugMode(isDebug: boolean) => any
开启 debug 模式 log日志
| Param | Type |
| ------------- | -------------------- |
| isDebug
| boolean |
Returns: any
setAlias(...)
setAlias(options: AliasOptions) => any
设置推送别名,可作为推送消息的目标对象
| Param | Type |
| ------------- | ----------------------------------------------------- |
| options
| AliasOptions |
Returns: any
deleteAlias(...)
deleteAlias(options?: DeleteAlias | undefined) => any
删除推送别名
| Param | Type |
| ------------- | --------------------------------------------------- |
| options
| DeleteAlias |
Returns: any
addTags(...)
addTags(options: SetTagsOptions) => any
设置推送标签
| Param | Type |
| ------------- | --------------------------------------------------------- |
| options
| SetTagsOptions |
Returns: any
deleteTags(...)
deleteTags(options: SetTagsOptions) => any
删除推送标签
| Param | Type |
| ------------- | --------------------------------------------------------- |
| options
| SetTagsOptions |
Returns: any
cleanTags()
cleanTags() => any
Returns: any
setBadgeNumber(...)
setBadgeNumber(options?: SetBadgeNumberOptions | undefined) => any
设置 APP 角标数字,设为 0 即清空角标
| Param | Type |
| ------------- | ----------------------------------------------------------------------- |
| options
| SetBadgeNumberOptions |
Returns: any
removeListeners()
removeListeners() => any
Returns: any
getRegistrationID()
getRegistrationID() => any
获取设备的注册 ID,若服务重新注册,则返回的 ID 是不一样的
Returns: any
checkPermissions()
checkPermissions() => any
检查通知权限状态
Returns: any
requestPermissions()
requestPermissions() => any
申请通知权限
Returns: any
openNotificationSetting()
openNotificationSetting() => any
打开推送通知权限设置页面(目前仅安卓支持)
Returns: any
addListener('notificationReceived', ...)
addListener(eventName: 'notificationReceived', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
监听推送消息
| Param | Type |
| ------------------ | ---------------------------------------------------------------------------------------------------------- |
| eventName
| 'notificationReceived' |
| listenerFunc
| (notificationData: ReceiveNotificationData) => void |
Returns: any
addListener('notificationOpened', ...)
addListener(eventName: 'notificationOpened', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
监听消息栏通知被点击
| Param | Type |
| ------------------ | ---------------------------------------------------------------------------------------------------------- |
| eventName
| 'notificationOpened' |
| listenerFunc
| (notificationData: ReceiveNotificationData) => void |
Returns: any
Interfaces
AliasOptions
| Prop | Type |
| -------------- | ------------------- |
| alias
| string |
| sequence
| number |
DeleteAlias
| Prop | Type |
| -------------- | ------------------- |
| sequence
| number |
SetTagsOptions
| Prop | Type |
| ---------- | --------------- |
| tags
| {} |
SetBadgeNumberOptions
| Prop | Type |
| ----------- | ------------------- |
| badge
| number |
PermissionStatus
| Prop | Type | Description |
| ---------------- | ----------------------------------------------------------- | ---------------------------------------------------------------------------- |
| permission
| PermissionState | prompt: 首次申请,询问。 prompt-with-rationale: 每次都询问。 granted: 已获取权限。 denied:权限已拒绝。 |
ReceiveNotificationData
| Prop | Type |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| title
| string |
| content
| string |
| subTitle
| string |
| rawData
| { [x: string]: any; aps: { alert: { body: string; subTitle: string; title: string; }; badge: number; sound: string; }; } |
PluginListenerHandle
| Prop | Type |
| ------------ | ------------------------- |
| remove
| () => any |
Type Aliases
PermissionState
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'