6.0.7 • Published 8 months ago

capacitor-plugin-jpushn v6.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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:

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

启动极光推送服务,即使没有获取到通知权限,也会进行推送服务初始化

Returns: any


setDebugMode(...)

setDebugMode(isDebug: boolean) => any

开启 debug 模式 log日志

ParamType
isDebugboolean

Returns: any


setAlias(...)

setAlias(options: AliasOptions) => any

设置推送别名,可作为推送消息的目标对象

ParamType
optionsAliasOptions

Returns: any


deleteAlias(...)

deleteAlias(options?: DeleteAlias | undefined) => any

删除推送别名

ParamType
optionsDeleteAlias

Returns: any


addTags(...)

addTags(options: SetTagsOptions) => any

设置推送标签

ParamType
optionsSetTagsOptions

Returns: any


deleteTags(...)

deleteTags(options: SetTagsOptions) => any

删除推送标签

ParamType
optionsSetTagsOptions

Returns: any


cleanTags()

cleanTags() => any

Returns: any


setBadgeNumber(...)

setBadgeNumber(options?: SetBadgeNumberOptions | undefined) => any

设置 APP 角标数字,设为 0 即清空角标

ParamType
optionsSetBadgeNumberOptions

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

监听推送消息

ParamType
eventName'notificationReceived'
listenerFunc(notificationData: ReceiveNotificationData) => void

Returns: any


addListener('notificationOpened', ...)

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

监听消息栏通知被点击

ParamType
eventName'notificationOpened'
listenerFunc(notificationData: ReceiveNotificationData) => void

Returns: any


Interfaces

AliasOptions

PropType
aliasstring
sequencenumber

DeleteAlias

PropType
sequencenumber

SetTagsOptions

PropType
tags{}

SetBadgeNumberOptions

PropType
badgenumber

PermissionStatus

PropTypeDescription
permissionPermissionStateprompt: 首次申请,询问。 prompt-with-rationale: 每次都询问。 granted: 已获取权限。 denied:权限已拒绝。

ReceiveNotificationData

PropType
titlestring
contentstring
subTitlestring
rawData{ x: string: any; aps: { alert: { body: string; subTitle: string; title: string; }; badge: number; sound: string; }; }

PluginListenerHandle

PropType
remove() => any

Type Aliases

PermissionState

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

6.0.7

8 months ago

6.0.6

8 months ago

6.0.5

8 months ago

6.0.4

8 months ago

6.0.3

8 months ago

6.0.2

8 months ago

6.0.1

8 months ago

6.0.0

8 months ago