import config from '@/config'
const jpushModule = uni.requireNativePlugin("JG-JPush")
import { checkNotifyPermission } from '@/utils/common'
const systemInfo = uni.getSystemInfoSync()
const {
platform,
deviceBrand
} = systemInfo
const tagList = ['C']
let registerID = ''
const sequence = 1
let innerAudioContext = null
const jPushStorageKey = 'jPushUnReadCount'
export const startJpush = (startQuery = {}) => {
if (!config.openJpush) {
return
}
const {
userId
} = startQuery
if (!userId) {
console.error('无用户userId,停止调用极光推送')
return
}
const openNotifyStatus = checkNotifyPermission()
console.log("userId", userId,openNotifyStatus);
if (!openNotifyStatus) {
console.error('没有授权,停止调用极光推送')
return
}
jpushModule.setLoggerEnable(true);
jpushModule.initJPushService();
jpushModule.addConnectEventListener(result => {
console.log("jpush连接", result)
})
const {
notificationAuthorized
} = uni.getAppAuthorizeSetting()
switch (notificationAuthorized) {
case 'denied':
let cancelNotify = uni.getStorageSync('cancelNotify')
if (cancelNotify !== '1') {
uni.showModal({
title: '通知提示',
content: '我们需要通知权限以便及时为您推送重要信息,是否前往打开?',
success: (res) => {
if (res.confirm) {
jpushModule.openSettingsForNotification()
} else if (res.cancel) {
console.log('用户点击取消');
uni.setStorageSync('cancelNotify', '1')
}
}
});
}
break;
case 'not determined':
if (platform === 'android') {
plus.android.requestPermissions(
['android.permission.POST_NOTIFICATIONS'],
function(result) {
console.log("开始获取权限", result);
},
function(error) {
console.error('检查权限出错:', error);
}
);
} else if (platform === 'ios') {
const UIApplication = plus.ios.import("UIApplication");
const app = UIApplication.sharedApplication();
if (app.currentUserNotificationSettings) {
const settings = app.currentUserNotificationSettings();
settings.plusGetAttribute("types");
plus.ios.deleteObject(settings);
} else {
app.enabledRemoteNotificationTypes();
}
plus.ios.deleteObject(app);
plus.ios.deleteObject(UIApplication);
}
break;
default:
break;
}
console.log('platform', platform,deviceBrand);
if (userId) {
jpushModule.addTags({
tags: tagList,
sequence
})
jpushModule.setAlias({
alias: userId,
sequence
})
}
if (platform === 'android' && deviceBrand==='xiaomi') {
jpushModule.setChannelAndSound({
channel: '订单语音提醒',
channel_id: 'xxx',
sound: 'sound'
})
}
if (platform === 'android' && deviceBrand==='oppo') {
jpushModule.setChannelAndSound({
channel: '抢单大厅来单',
channel_id: 'xxx',
sound: 'sound'
})
}
if (platform === 'android' && deviceBrand==='huawei') {
jpushModule.setChannelAndSound({
channel: '订单语音提醒',
channel_id: 'xxx',
sound: 'sound'
})
}
jpushModule.setBadge(0)
jpushModule.addNotificationListener(result => {
console.log("通知事件回调", result);
const {
messageID,
title,
content,
extras,
iOS,
android,
notificationEventType
} = result
switch (notificationEventType) {
case 'notificationArrived':
if (extras?.voice) {
if (innerAudioContext) {
try {
innerAudioContext.pause();
innerAudioContext.destroy()
innerAudioContext = null
} catch (e) {
}
}
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = false;
innerAudioContext.src = extras.voice
innerAudioContext.play()
innerAudioContext.onPlay(() => {
console.log('开始播放');
});
innerAudioContext.onError((res) => {
console.error('播放错误', res);
});
}
break;
case 'notificationOpened':
const pageUrl = extras?.pageUrl
const notifyType = extras?.notifyType
let navigateKeys = ['navigateTo', 'switchTab']
if (navigateKeys.includes(notifyType) && pageUrl) {
switch (notifyType) {
case 'navigateTo':
uni.navigateTo({
url: pageUrl
})
break;
case 'switchTab':
uni.switchTab({
url: pageUrl
})
break;
default:
break;
}
}
break;
default:
break;
}
});
}