极光推送Cordova插件适配Capacitor

极光推送官方提供的Cordova插件目前无法适配Capacitor电容器平台进行开发,之前问了官方什么时候会考虑适配,官方的回答是暂不做开发安排,需要调研,这对于想使用Capacitor进行开发的同学来说不太友好,Google了一番,发现有一个GitHub已经对此做了适配,于是Fork了一份整理了下,附上使用文档。原GitHub传送门:https://github.com/netsesame2/cordova-plugin-jpush

Capacitor 使用 npm 安装:

npm i cordova-plugin-jpush-capacitor

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

ionic cap sync

IOS 设置APPKEY:

ionic cap add ios
ionic cap sync

Xcode打开IOS项目,找到如下图中的jpush配置文件,替换成自己项目的APPKEY:
在这里插入图片描述

Android设置APPKEY:

ionic cap add android
ionic cap sync

AndroidStudio打开生成的Android项目,找到如下图的标记文件,将APPKEY替换成自己的:
在这里插入图片描述
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')) {
                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

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>

由于Capacitor生成的IOS平台自动加入了推送,会导致极光插件获取的DeviceToken无法回传至极光服务器,所以,还需对IOS进行修改:

  1. 添加IOS平台
ionic cap add ios
ionic cap sync
  1. 用Xcode打开项目
ionic cap open ios

在工程中新建一个 Objective-C Bridging Header 文件:
在这里插入图片描述
在刚生成的Objective-C Bridging Header文件中导入 jpush 头文件
在这里插入图片描述
在Appdelegate.swift 文件的 didRegisterForRemoteNotificationsWithDeviceToken 方法中添加如下代码:

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

另外,在Target中需要加入推送通知权限:

在这里插入图片描述
官方Cordova插件ISSUE:https://github.com/jpush/jpush-phonegap-plugin/issues/434

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

废柴前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值