Uniapp极光推送常用方法封装

作者:renzp94
时间:2021-07-07 02:14:43

常用方法封装是针对Uniapp插件市场中的极光官方的两个插件:极光JPush官方SDK极光JCore官方SDK,关于安装请参考Uniapp 对接极光推送

jpush.js

import { isDev, isIos, isAndroid } from './tools';
import store from '@/store';
import storage, { MESSAGE_TOTAL, USER_INFO, CLOSE_NOTIFICATION_TIPS } from './storage';
import { goMessages } from '@/utils/pageHelper';

// 不在App内提醒
const unAppToast = () => {
    // #ifndef APP-PLUS
    isDev &&
        uni.showToast({
            title: '极光推送仅限APP内使用',
            icon: 'none'
        });
    // #endif
};
// 初始化极光推送
export const jpushInit = () => {
    // #ifdef APP-PLUS
    const jv = uni.requireNativePlugin('JG-JPush');
    jv.initJPushService();
    jv.addConnectEventListener((result) => {
        if (result.connectEnable) {
            isDev && console.log('连接极光推送成功');
            const userinfo = storage.get(USER_INFO);
            if (userinfo) {
                let total = 0;
                if (isIos) {
                    const app = plus.ios.import('UIApplication').sharedApplication();
                    total = app.applicationIconBadgeNumber();
                }

                if (total === 0) {
                    let total = storage.get(`${MESSAGE_TOTAL}_${userinfo.id}`);
                    total = total ? total : 0;
                }
                +store.commit('updateMessageTotal', total);
                storage.set(`${MESSAGE_TOTAL}_${userinfo.id}`, total);
            }
        }
    });
    const closeNotificationTips = storage.get(CLOSE_NOTIFICATION_TIPS);
    if (!closeNotificationTips) {
        getNotificationAuthorization();
    }
    onNotification();
    // #endif
    unAppToast();
};
// 监听消息推送
const onNotification = () => {
    const jv = uni.requireNativePlugin('JG-JPush');
    jv.addNotificationListener((data) => {
        const { badge, notificationEventType } = data;
        // 收到新消息
        if (notificationEventType === 'notificationArrived') {
            setBadge(badge);
        }
        // 点击打开消息
        if (notificationEventType === 'notificationOpened') {
            if (store.state.isLaunch) {
                goMessages();
            } else {
                store.commit('updateIsGoMessage', true);
            }
        }
    });
};
// 查询是否有通知权限
const getNotificationAuthorization = () => {
    const jv = uni.requireNativePlugin('JG-JPush');
    if (isAndroid) {
        jv.isNotificationEnabled(({ code }) => {
            if (code === 0) {
                goNotificationSettings();
            }
        });
    }
};
// 跳转消息通知设置界面
export const goNotificationSettings = () => {
    uni.showModal({
        content: '检测到您未打开消息通知,开启后第一时间可收到消息推送',
        cancelText: '稍后开启',
        confirmText: '去开启',
        success: function (res) {
            storage.set(CLOSE_NOTIFICATION_TIPS, true);
            if (res.confirm) {
                const jv = uni.requireNativePlugin('JG-JPush');
                jv.openSettingsForNotification();
            }
        }
    });
};
// 设置应用消息量角标
export const setBadge = (badge, isSyncData = true) => {
    // #ifdef APP-PLUS
    plus.runtime.setBadgeNumber(badge);
    const jv = uni.requireNativePlugin('JG-JPush');
    jv.setBadge(badge);
    if (isSyncData) {
        const userinfo = storage.get(USER_INFO);
        storage.set(`${MESSAGE_TOTAL}_${userinfo?.id}`, badge);
        store.commit('updateMessageTotal', badge);
    }
    // #endif
    unAppToast();
};
// 设置别名
export const setAlias = (alias) => {
    // #ifdef APP-PLUS
    const jv = uni.requireNativePlugin('JG-JPush');
    console.log('alias', alias);
    jv.setAlias({ alias });
    // #endif
    unAppToast();
};
// 删除别名
export const removeAlias = () => {
    // #ifdef APP-PLUS
    const jv = uni.requireNativePlugin('JG-JPush');
    jv.deleteAlias({});
    // #endif
    unAppToast();
};