简体中文 | English
jpush plugin for capacitor3.0+
support
Capacitor 5
npm install capacitor-plugin-jpush
npx cap sync
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": ""
}
}
}
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 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
:
add the following to your app's build.gradle
:
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
]
Currently does not support the manufacturer channel push
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);
// ......
};
startJPush()
setDebugMode(...)
setAlias(...)
deleteAlias(...)
addTags(...)
deleteTags(...)
cleanTags()
setBadgeNumber(...)
removeListeners()
getRegistrationID()
checkPermissions()
requestPermissions()
openNotificationSetting()
addListener('notificationReceived', ...)
addListener('notificationOpened', ...)
- Interfaces
- Type Aliases
startJPush() => Promise<void>
start JPush service
setDebugMode(isDebug: boolean) => Promise<void>
enable JPush debug log
Param | Type |
---|---|
isDebug |
boolean |
setAlias(options: AliasOptions) => Promise<void>
set alias for JPush
Param | Type |
---|---|
options |
AliasOptions |
deleteAlias(options?: DeleteAlias | undefined) => Promise<void>
Param | Type |
---|---|
options |
DeleteAlias |
addTags(options: SetTagsOptions) => Promise<void>
Param | Type |
---|---|
options |
SetTagsOptions |
deleteTags(options: SetTagsOptions) => Promise<void>
Param | Type |
---|---|
options |
SetTagsOptions |
cleanTags() => Promise<void>
setBadgeNumber(options?: SetBadgeNumberOptions | undefined) => Promise<void>
Param | Type |
---|---|
options |
SetBadgeNumberOptions |
removeListeners() => Promise<void>
getRegistrationID() => Promise<{ registrationId: string; }>
Returns: Promise<{ registrationId: string; }>
checkPermissions() => Promise<PermissionStatus>
Returns: Promise<PermissionStatus>
requestPermissions() => Promise<PermissionStatus>
Returns: Promise<PermissionStatus>
openNotificationSetting() => Promise<void>
now only on Android
addListener(eventName: 'notificationReceived', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'notificationReceived' |
listenerFunc |
(notificationData: ReceiveNotificationData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener(eventName: 'notificationOpened', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'notificationOpened' |
listenerFunc |
(notificationData: ReceiveNotificationData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Prop | Type |
---|---|
alias |
string |
sequence |
number |
Prop | Type |
---|---|
sequence |
number |
Prop | Type |
---|---|
tags |
string[] |
Prop | Type |
---|---|
badge |
number |
Prop | Type |
---|---|
permission |
PermissionState |
Prop | Type |
---|---|
remove |
() => Promise<void> |
Prop | Type |
---|---|
title |
string |
content |
string |
subTitle |
string |
rawData |
{ [x: string]: any; aps: { alert: { body: string; subTitle: string; title: string; }; badge: number; sound: string; }; } |
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'