Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capacitor集成JPUSH的看这里!!! #434

Open
AlwaysLoveme opened this issue Oct 28, 2020 · 39 comments
Open

Capacitor集成JPUSH的看这里!!! #434

AlwaysLoveme opened this issue Oct 28, 2020 · 39 comments

Comments

@AlwaysLoveme
Copy link

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

@AlwaysLoveme
Copy link
Author

AlwaysLoveme commented Oct 28, 2020

为了方便使用,我将它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>

@conkyliu
Copy link

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

@menhal
Copy link

menhal commented Oct 29, 2020

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

@conkyliu
Copy link

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

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

@menhal
Copy link

menhal commented Oct 29, 2020

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

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

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

@conkyliu
Copy link

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

苍天,还是不行

@AlwaysLoveme
Copy link
Author

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

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

@menhal
Copy link

menhal commented Oct 30, 2020

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

@conkyliu
Copy link

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

我引入报错啊

@menhal
Copy link

menhal commented Oct 30, 2020

好用了!!! , 参考极光官方的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,操作更方便哦

@conkyliu
Copy link

好用了!!! , 参考极光官方的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的成功失败事件,你的会吗?现在只能靠订单支付轮询跳转

@AlwaysLoveme
Copy link
Author

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

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

@AlwaysLoveme
Copy link
Author

@menhal
Copy link

menhal commented Nov 5, 2020

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

给你点赞

@conkyliu
Copy link

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

@AlwaysLoveme
Copy link
Author

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

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

@conkyliu
Copy link

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

@AlwaysLoveme
Copy link
Author

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

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

@conkyliu
Copy link

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

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

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

@w8w8w8
Copy link

w8w8w8 commented Aug 3, 2021

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

完全获取不到 Registration ID,

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

没有你说的这个方法呢?

@AlwaysLoveme
Copy link
Author

@jiguang-ionic/jpush/ngx

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

@soweibo
Copy link

soweibo commented Aug 3, 2021

支持厂商通道吗?

@w8w8w8
Copy link

w8w8w8 commented Aug 4, 2021

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

@AlwaysLoveme
Copy link
Author

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

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

@AlwaysLoveme
Copy link
Author

支持厂商通道吗?

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

@speykye
Copy link

speykye commented Sep 7, 2021

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

@AlwaysLoveme
Copy link
Author

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

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

@w8w8w8
Copy link

w8w8w8 commented Oct 22, 2021

@AlwaysLoveme I see, 谢谢你

@chy89310
Copy link

我运行下面命令安装该插件并同步到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

请问有人遇到过吗?

@ahhxyz
Copy link

ahhxyz commented Dec 4, 2022

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

@AlwaysLoveme
Copy link
Author

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

在 node_modules 里面改APP_KEY,

@ahhxyz
Copy link

ahhxyz commented Dec 7, 2022

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

在 node_modules 里面改APP_KEY,

node_modules对应包中没找到KEY的配置。
后面找到办法了,我在main\AndroidManifest.xml中添加了:
<meta-data android:name="JPUSH_APPKEY" android:value="xxxxxx" tools:replace="android:value" />
它会替换capacitor-cordova-android-plugins下AndroidManifest.xml中的JPUSH_KEY配置。
现在能收到推送消息了。
非常感谢,这个issue帮了大忙。

@AlwaysLoveme
Copy link
Author

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

在 node_modules 里面改APP_KEY,

node_modules对应包中没找到KEY的配置。 后面找到办法了,我在main\AndroidManifest.xml中添加了: <meta-data android:name="JPUSH_APPKEY" android:value="xxxxxx" tools:replace="android:value" />, 它会替换capacitor-cordova-android-plugins下AndroidManifest.xml中的JPUSH_KEY配置。 现在能收到推送消息了。 非常感谢,这个issue帮了大忙。

OK,不客气

@sea-forever-zh
Copy link

sea-forever-zh commented Dec 15, 2022

@AlwaysLoveme 你好,我用的是ionic6.3.9+capacitor4.6.1+ng14.1.0,添加cordova-plugin-jpush-capacitor之后(没有其它jpush依赖),执行ionic cap sync,重新打包时,提示这个错误
image

是还需要装jpush的相关依赖么?

@sea-forever-zh
Copy link

@AlwaysLoveme 你好,我用的是ionic6.3.9+capacitor4.6.1+ng14.1.0,添加cordova-plugin-jpush-capacitor之后(没有其它jpush依赖),执行ionic cap sync,重新打包时,提示这个错误 image

是还需要装jpush的相关依赖么?

我在npm中找到原因了cordova-plugin-jpush-capacitor

安装了这两个插件后。可以正常打包了
image

@sea-forever-zh
Copy link

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

在 node_modules 里面改APP_KEY,

可以在capacitor.config.ts中配置么?就像@capacitor/splash-screen
image

@AlwaysLoveme
Copy link
Author

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

在 node_modules 里面改APP_KEY,

可以在capacitor.config.ts中配置么?就像@capacitor/splash-screen image

这个暂不支持,因为插件还是用 cordova 开发的,无法支持在 capacitor.config.ts 读取,除非基于 Capacitor重新开发此插件

@sea-forever-zh
Copy link

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

在 node_modules 里面改APP_KEY,

可以在capacitor.config.ts中配置么?就像@capacitor/splash-screen image

这个暂不支持,因为插件还是用 cordova 开发的,无法支持在 capacitor.config.ts 读取,除非基于 Capacitor重新开发此插件

好的,٩( 'ω' )و 蟹蟹

@AlwaysLoveme
Copy link
Author

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

在 node_modules 里面改APP_KEY,

可以在capacitor.config.ts中配置么?就像@capacitor/splash-screen image

这个暂不支持,因为插件还是用 cordova 开发的,无法支持在 capacitor.config.ts 读取,除非基于 Capacitor重新开发此插件

好的,٩( 'ω' )و 蟹蟹

我基于capacitor重新开发了此插件,欢迎使用,https://github.com/AlwaysLoveme/capacitor-plugin-jpush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants