fei.wang
2025-04-30 722da005a5ec126bedf752ac6bd5c5c7f6172155
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function () {
 
    function pushReady() {
 
        function pushToPage(anc) {
            plus.nativeUI.toast('push jump:' + JSON.stringify(anc))
            const url = '/pushFilter?pushId=' + anc.id + '&pushType=' + anc.type + '&pushUrl=' + anc.url + '&title=' + (!anc.content ? anc.msg : anc.content)
 
            uni.navigateTo({
                url: url
            })
 
        }
 
        plus.nativeUI.toast('plus ready')
 
        plus.push.addEventListener('click', function (msg) {
            // IOS后台的消息是msg.payload对象,前台的消息是msg.payload字符串
            plus.nativeUI.toast('click:' + JSON.stringify(msg))
            if (msg.payload.id == null) {
                pushToPage(JSON.parse(msg.payload))
            } else {
                pushToPage(msg.payload)
            }
        })
 
 
        // 安卓的推送,点击后触发receive,IOS的通知点击的时候触发click监听器
        // ios使用透传,接收后触发receive
        plus.push.addEventListener('receive', function (msg) {
            plus.nativeUI.toast('receive:' + JSON.stringify(msg))
            let payload = msg.payload
            // IOS接收后创建本地消息,本地消息会再次触发receive,所以判断是否本地创建的,将其它数据封装在payload(需为字符串)中
            // 直发时,content = payload = {}
            if (plus.os.name !== 'Android' && msg.type === 'receive') {
                let content = payload.msg
                let tit = payload.title
                let pl = {
                    'title': tit,
                    'content': content,
                    'id': payload.id,
                    'type': payload.type,
                    'url': payload.url
                }
                plus.push.createMessage(content, JSON.stringify(pl), {'title': tit})
            }
 
            if (plus.os.name === 'Android' && msg.content.indexOf('{') === 0) {
                plus.nativeUI.toast('payload:' + payload)
                payload = JSON.parse(payload)
                let content = payload.msg
                let tit = payload.title
                let pl = {
                    'title': tit,
                    'content': content,
                    'id': payload.id,
                    'type': payload.type,
                    'url': payload.url
                }
                plus.nativeUI.toast(content + '_' + JSON.stringify(pl))
                plus.push.createMessage(content, JSON.stringify(pl), {'title': tit})
            }
 
            if (plus.os.name === 'Android' && msg.content.indexOf('{') === -1) {
 
                let anc = JSON.parse(msg.payload)
 
                let pl = {
                    'title': msg.title,
                    'content': msg.content,
                    'id': anc.id,
                    'type': anc.type,
                    'url': anc.url
                }
                plus.push.createMessage(msg.content, JSON.stringify(pl), {'title': msg.title})
            }
        })
    }
 
    //#ifdef APP-PLUS
    pushReady()
    //#endif
})()