Skip to content

Latest commit

 

History

History
437 lines (290 loc) · 12.4 KB

README-EN.md

File metadata and controls

437 lines (290 loc) · 12.4 KB

capacitor-plugin-jpush

简体中文 | English

jpush plugin for capacitor3.0+

support Capacitor 5

Install

npm install capacitor-plugin-jpush
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:

https://user-images.githubusercontent.com/29945352/235104201-a39bdb6e-314d-423a-beb7-2869f3b27679.png

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()

startJPush() => Promise<void>

start JPush service


setDebugMode(...)

setDebugMode(isDebug: boolean) => Promise<void>

enable JPush debug log

Param Type
isDebug boolean

setAlias(...)

setAlias(options: AliasOptions) => Promise<void>

set alias for JPush

Param Type
options AliasOptions

deleteAlias(...)

deleteAlias(options?: DeleteAlias | undefined) => Promise<void>
Param Type
options DeleteAlias

addTags(...)

addTags(options: SetTagsOptions) => Promise<void>
Param Type
options SetTagsOptions

deleteTags(...)

deleteTags(options: SetTagsOptions) => Promise<void>
Param Type
options SetTagsOptions

cleanTags()

cleanTags() => Promise<void>

setBadgeNumber(...)

setBadgeNumber(options?: SetBadgeNumberOptions | undefined) => Promise<void>
Param Type
options SetBadgeNumberOptions

removeListeners()

removeListeners() => Promise<void>

getRegistrationID()

getRegistrationID() => Promise<{ registrationId: string; }>

Returns: Promise<{ registrationId: string; }>


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Returns: Promise<PermissionStatus>


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

Returns: Promise<PermissionStatus>


openNotificationSetting()

openNotificationSetting() => Promise<void>

now only on Android


addListener('notificationReceived', ...)

addListener(eventName: 'notificationReceived', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName 'notificationReceived'
listenerFunc (notificationData: ReceiveNotificationData) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


addListener('notificationOpened', ...)

addListener(eventName: 'notificationOpened', listenerFunc: (notificationData: ReceiveNotificationData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName 'notificationOpened'
listenerFunc (notificationData: ReceiveNotificationData) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


Interfaces

AliasOptions

Prop Type
alias string
sequence number

DeleteAlias

Prop Type
sequence number

SetTagsOptions

Prop Type
tags string[]

SetBadgeNumberOptions

Prop Type
badge number

PermissionStatus

Prop Type
permission PermissionState

PluginListenerHandle

Prop Type
remove () => Promise<void>

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; }; }

Type Aliases

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'