Coder Social home page Coder Social logo

oooh-name / cordova-plugin-jpush Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alwaysloveme/cordova-plugin-jpush-capacitor

1.0 0.0 0.0 94.6 MB

JPush plugin for Capacitor(2.0/3.0)

Home Page: https://github.com/jpush/jpush-phonegap-plugin

License: MIT License

JavaScript 53.47% C 0.25% Objective-C 14.67% Java 7.36% CSS 22.52% HTML 1.73%

cordova-plugin-jpush's Introduction

cordova-plugin-jpush-capacitor

platforms weibo

在官方基础上,增加对Capacitor电容器的支持(支持Capacitor3)

Capacitor(2.0/3.0) 安装

npm install cordova-plugin-jpush-capacitor -S

npm install cordova-plugin-device -S

npm install cordova-plugin-jcore -S

同步插件至Native项目:

npx cap sync

IOS 使用

配置JPUSH APPkey: https://img-blog.csdnimg.cn/2020110211285992.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXhpYW5kYW4=,size_16,color_FFFFFF,t_70#pic_center 也可以在安装npm包之后,在插件目录 node_modules/cordova-plugin-jpush-capacitor/src/ios/JPushConfig.plist 中修改,推荐使用这个方式更改,更改之后需要手动在执行一遍npx cap sync

IOS项目中添加桥接头文件, 如图: https://img-blog.csdnimg.cn/20201102105208957.gif#pic_center 生成的.m文件可以删除

在生成的头文件中导入jpush:

#import "JPUSHService.h"

Capacitor 注册极光devicetoken

Capacitor2.0版本: 在Appdelegate.swift 文件的 didRegisterForRemoteNotificationsWithDeviceToken 方法中添加如下代码:

func application(application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  NSNotificationCenter.defaultCenter().postNotificationName("DidRegisterRemoteNotification", object: deviceToken)
  JPUSHService.registerDeviceToken(deviceToken)
}

Capacitor3.0版本: 在Appdelegate.swift中加入以下代码:

func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  JPUSHService.registerDeviceToken(deviceToken)
}

以上准备就绪后,需要手动调用初始化SDK:

window.JPush.startJPushSDK() // 可以打印一下window.JPush, 官方支持的API均可以使用

Android 使用

配置JPUSH APPkey: Android 配置APPKey 也可以在安装npm包之后,在插件目录 node_modules/cordova-plugin-jpush-capacitor/plugin.xml 中修改,推荐使用这个方式更改

// 在plugin.xml中将此处的$APP_KEY替换成自己的即可
<meta-data android:name="JPUSH_APPKEY" android:value="$APP_KEY" /> 

替换之后需要手动在执行一遍npx cap sync,直接在原生项目中做替换的可不必运行

Android无需手动初始化SDK

示例代码(以Ionic为例)

import { isPlatform } from '@ionic/vue';
class Jpush {
    jpush: any;

    constructor() {
        if (window.JPush) {
            this.jpush = window.JPush;
            this.jpush.setDebugMode(true);
            if (isPlatform('ios')) {
                this.jpush.startJPushSDK();
            }
            this.jpush.init();
        }
    }

    getRegistrationID() {
        return new Promise(resolve => {
            this.jpush.getRegistrationID(function (rId: string) {
                resolve(rId);
                console.log("JPushPlugin:registrationID is " + rId);
            })
        })
    }

    // 设置别名
    setAlias(alias: string) {
      return new Promise(((resolve, reject) => {
        this.jpush.setAlias({
          alias,
          sequence: new Date().valueOf(),
        },  (res: { alias: string, sequence: number }) => {
          console.log('别名设置成功: ', res);
          resolve(res);
        },  (err: { code: number, sequence: number }) => {
            console.log('别名设置失败: ', err);
            setTimeout(() => this.setAlias(alias), 3000);
            reject(err);
        })
      }))

    }

    // 设置角标 只限IOS
    setBadge(badge: number) {
      if (isPlatform('ios')) {
        this.jpush.setBadge(badge);
      }
    }
}
export default Jpush

API请查阅官方文档:https://github.com/jpush/jpush-phonegap-plugin

cordova-plugin-jpush's People

Contributors

zhangqinghe avatar pikacode avatar huangminlinux avatar lhw5123 avatar joshlipan avatar lincolnlk avatar darkterrorooo avatar wgjtyu avatar weiruiyang avatar hailiang-wang avatar reyoung avatar javenfang avatar withwind8 avatar annilq avatar yaonie084 avatar netsesame2 avatar

Stargazers

Roman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.