jpush-phonegap-plugin icon indicating copy to clipboard operation
jpush-phonegap-plugin copied to clipboard

Capacitor集成JPUSH的看这里!!!

Open AlwaysLoveme opened this issue 5 years ago • 39 comments

最近找到一个大牛写的适配capacitor的插件,传送门:https://github.com/netsesame2/cordova-plugin-jpush

AlwaysLoveme avatar Oct 28 '20 09:10 AlwaysLoveme

为了方便使用,我将它Fork了一份,感谢大牛!!! Capacitor 使用 npm 安装:

npm i cordova-plugin-jpush-capacitor@latest

capacitor同步插件至Android/iOS项目(前提是已经通过命令添加了IOS/Android平台):

ionic cap sync

IOS 设置APPKEY:

ionic cap add ios
ionic cap sync

Xcode打开IOS项目,找到如下图中的jpush配置文件,替换成自己项目的APPKEY: image

Android设置APPKEY:

ionic cap add android
ionic cap sync

AndroidStudio打开生成的Android项目,找到如下图的标记文件,将APPKEY替换成自己的: image

TS中使用,我是用ionic+vue3+capacitor, 纯属用来试水,新建src/utils/jpush.ts文件:

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

    constructor() {
        if (window.JPush) {
            this.jpush = window.JPush;
            this.jpush.setDebugMode(true);
            if (isPlatform('ios')) {
                window.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

App.vue中使用:

<template>
  <ion-app>
    <ion-router-outlet />
  </ion-app>
</template>

<script lang="ts">
import { Plugins } from "@capacitor/core";
import { defineComponent, onMounted } from 'vue';
import { IonApp, IonRouterOutlet } from '@ionic/vue';
import jpush from "@/utils/jpush";
const { SplashScreen } = Plugins;

export default defineComponent({
  name: 'App',
  components: {
    IonApp,
    IonRouterOutlet
  },
  setup() {
    onMounted(() => {
      // 由于是cordova插件,需要在deviceready回调后才能使用,用过Cordova的都懂
      document.addEventListener('deviceready', () => {
        new jpush().setAlias('app');
      })
      setTimeout(() => SplashScreen.hide(), 2000);
    })
  }
});
</script>

AlwaysLoveme avatar Oct 28 '20 10:10 AlwaysLoveme

我测试下,AndroidManifest.xml的配置放在cordova的话应用进入后台就接收不到推送,放在app的话就可以

conkyliu avatar Oct 29 '20 03:10 conkyliu

ios下能获取Registration ID, 但是无法收到通知... 后台提示消息送达了, 但是手机上没有通知. 按照官方文档说的各种办法试了不好用.

menhal avatar Oct 29 '20 06:10 menhal

ios下能获取Registration ID, 但是无法收到通知... 后台提示消息送达了, 但是手机上没有通知. 按照官方文档说的各种办法试了不好用.

为啥我获取不到,安卓正常

conkyliu avatar Oct 29 '20 07:10 conkyliu

ios下能获取Registration ID, 但是无法收到通知... 后台提示消息送达了, 但是手机上没有通知. 按照官方文档说的各种办法试了不好用.

为啥我获取不到,安卓正常

ios比Android多一句这个 window.JPush.startJPushSDK();

menhal avatar Oct 29 '20 08:10 menhal

ios下能获取Registration ID, 但是无法收到通知... 后台提示消息送达了, 但是手机上没有通知. 按照官方文档说的各种办法试了不好用.

为啥我获取不到,安卓正常

ios比Android多一句这个 window.JPush.startJPushSDK();

To Native Cordova -> JPushPlugin startJPushSDK JPushPlugin19045247 ["options": []] To Native Cordova -> JPushPlugin initial JPushPlugin19045248 ["options": []] To Native Cordova -> JPushPlugin getRegistrationID JPushPlugin19045249 ["options": []] ⚡️ [log] - JPush Callback Error: null

苍天,还是不行

conkyliu avatar Oct 29 '20 08:10 conkyliu

ios下能获取Registration ID, 但是无法收到通知... 后台提示消息送达了, 但是手机上没有通知. 按照官方文档说的各种办法试了不好用.

今天我也发现了,昨天刚集成好的时候,在极光后台推送了,IOS能正常接收到推送,今天不行了,Xcode控制台看了下,可能是DeviceToken 一直获取不到,没有回传给极光后台;

AlwaysLoveme avatar Oct 29 '20 10:10 AlwaysLoveme

好用了!!! , 参考极光官方的swift文档, 在根目录APPDelegate.swift中调用极光的registerDeviceToken方法 https://github.com/jpush/jpush-swift-demo

menhal avatar Oct 30 '20 03:10 menhal

好用了!!! , 参考极光官方的swift文档, 在根目录APPDelegate.swift中调用极光的registerDeviceToken方法 https://github.com/jpush/jpush-swift-demo

我引入报错啊

conkyliu avatar Oct 30 '20 03:10 conkyliu

好用了!!! , 参考极光官方的swift文档, 在根目录APPDelegate.swift中调用极光的registerDeviceToken方法 https://github.com/jpush/jpush-swift-demo

我引入报错啊

需要按照文档生成bridge文件, 并在bridge中引入JPushService.h

参见我的工程 链接: https://pan.baidu.com/s/1Aj2j_FpQH8-tYCq7b7lujg 提取码: bqbg 复制这段内容后打开百度网盘手机App,操作更方便哦

menhal avatar Oct 30 '20 05:10 menhal

好用了!!! , 参考极光官方的swift文档, 在根目录APPDelegate.swift中调用极光的registerDeviceToken方法 https://github.com/jpush/jpush-swift-demo

我引入报错啊

需要按照文档生成bridge文件, 并在bridge中引入JPushService.h

参见我的工程 链接: https://pan.baidu.com/s/1Aj2j_FpQH8-tYCq7b7lujg 提取码: bqbg 复制这段内容后打开百度网盘手机App,操作更方便哦

感谢 感谢,终于可以了,像微信,支付宝支付完了回到app,capacitor不会执行js的成功失败事件,你的会吗?现在只能靠订单支付轮询跳转

conkyliu avatar Oct 30 '20 06:10 conkyliu

好用了!!! , 参考极光官方的swift文档, 在根目录APPDelegate.swift中调用极光的registerDeviceToken方法 https://github.com/jpush/jpush-swift-demo

可以的,我整合一下,多谢

AlwaysLoveme avatar Nov 02 '20 02:11 AlwaysLoveme

文档博客:https://blog.csdn.net/zhuxiandan/article/details/109443648

AlwaysLoveme avatar Nov 02 '20 03:11 AlwaysLoveme

文档博客:https://blog.csdn.net/zhuxiandan/article/details/109443648

给你点赞

menhal avatar Nov 05 '20 01:11 menhal

image ios的你们有报这个警告吗

conkyliu avatar Nov 20 '20 10:11 conkyliu

image ios的你们有报这个警告吗

有的,但是这个应该不影响业务,除非你要统计通知的点击量

AlwaysLoveme avatar Nov 23 '20 01:11 AlwaysLoveme

image image image 真机运行正常,模拟器就会报错,后面注释了这块函数才可以运行

conkyliu avatar Jun 21 '21 06:06 conkyliu

image image image 真机运行正常,模拟器就会报错,后面注释了这块函数才可以运行

推送你在真机调试啊,为什么要在模拟器调试推送呢

AlwaysLoveme avatar Jun 22 '21 03:06 AlwaysLoveme

image image image 真机运行正常,模拟器就会报错,后面注释了这块函数才可以运行

推送你在真机调试啊,为什么要在模拟器调试推送呢

不是要调试,是一启动应用就卡死了

conkyliu avatar Jun 22 '21 03:06 conkyliu

@menhal 我安装的 这个插件,没有 startJPushSDK() 方法呢?

完全获取不到 Registration ID,

import { JPush } from '@jiguang-ionic/jpush/ngx'; image

没有你说的这个方法呢?

w8w8w8 avatar Aug 03 '21 01:08 w8w8w8

@jiguang-ionic/jpush/ngx

@jiguang-ionic/jpush/ngx 这个包没有暴露出这个方法,不适用于Capacitor,

AlwaysLoveme avatar Aug 03 '21 07:08 AlwaysLoveme

支持厂商通道吗?

soweibo avatar Aug 03 '21 09:08 soweibo

@AlwaysLoveme 请问,我用ionic 6 + Capacitor +angular 怎么才能 设置 startJPushSDK() 方法呢?

w8w8w8 avatar Aug 04 '21 00:08 w8w8w8

@AlwaysLoveme 请问,我用ionic 6 + Capacitor +angular 怎么才能 设置 startJPushSDK() 方法呢?

集成了上面我说的那个改过的极光插件,在deviceready事件中直接window.JPush.startJPushSDK() 就可以,android平台不需要手动初始化

AlwaysLoveme avatar Aug 05 '21 01:08 AlwaysLoveme

支持厂商通道吗?

厂商通道没试过,应该是可以的,

AlwaysLoveme avatar Aug 05 '21 01:08 AlwaysLoveme

类型“Window & typeof globalThis”上不存在属性“JPush”,这个问题怎么解决?

speykye avatar Sep 07 '21 06:09 speykye

类型“Window & typeof globalThis”上不存在属性“JPush”,这个问题怎么解决?

简单粗暴的方法:(window as any).JPush 或者定义类型:interface Window { JPush: any }

AlwaysLoveme avatar Oct 20 '21 01:10 AlwaysLoveme

@AlwaysLoveme I see, 谢谢你

w8w8w8 avatar Oct 22 '21 00:10 w8w8w8

我运行下面命令安装该插件并同步到iOS npm i cordova-plugin-jpush-capacitor@latest ionic cap sync ios

在XCode编译的时候就会报 Undefined symbols for architecture x86_64: "OBJC_CLASS$_JCORELog", referenced from: objc-class-ref in libjpush-ios-3.7.4.a(JPUSHService.o) objc-class-ref in libjpush-ios-3.7.4.a(JPUSHHelper.o) "OBJC_CLASS$_JCOREIntegrate", referenced from: objc-class-ref in libjpush-ios-3.7.4.a(JPUSHService.o) "_kJPFNetworkDidReceiveMessageNotification", referenced from: -[JPushPlugin initPlugin] in JPushPlugin.o

请问有人遇到过吗?

chy89310 avatar Jan 12 '22 17:01 chy89310

android\capacitor-cordova-android-plugins\src\main\AndroidManifest.xml 我项目的这个文件,修改后,运行 capacitor run 时,总是会被重新生成,里面设置的APP_KEY会被替换成undefined,这个该怎么解决啊。

ahhxyz avatar Dec 04 '22 10:12 ahhxyz