App-Android(使用App+htnl5框架,解决消息推送兼容SignalR问题)
loulijun2021
2022-09-18 e5d34f5c51e4a852e67d24709ec7e7b708846066
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <meta name="HandheldFriendly" content="true"/>
        <meta name="MobileOptimized" content="320"/>
        <title>Hello H5+</title>
        <script type="text/javascript" src="../js/common.js"></script>
        <script type="text/javascript">
/*    
NSNotificationCenter *nc = nil;
 
// 游戏玩家登录状态监听函数
- (void)authenticationChanged:(NSNotification*)notification
{
    // 获取游戏玩家共享实例对象
    GKLocalPlayer *player = notification.object;
    if ( player.isAuthenticated ) {
        // 玩家已登录认证,获取玩家信息
        [self playerInformation:player];
    } else {
        // 玩家未登录认证,提示用户登录
        NSLog(@"请登录!");
    }
    // 释放使用的对象
    [player release];
}
 
// 获取游戏玩家状态信息
- (void)playerInformation:(GKPlayer *)player
{
    // 获取游戏玩家的名称
    NSLog(@"Name: %@",player.displayName);
}
 
// 登录到游戏中心
- (void)loginGamecenter
{
    // 获取游戏玩家共享实例对象
    GKLocalPlayer *localplayer = [GKLocalPlayer localPlayer];
    // 判断游戏玩家是否已经登录认证
    if ( localplayer.isAuthenticated ) {
        // 玩家已登录认证,获取玩家信息
        [self playerInformation:localplayer];
    } else {
        // 监听用户登录状态变更事件
        if(!nc){
            nc = [NSNotificationCenter defaultCenter];
            [nc addObserver:self
                   selector:@selector(authenticationChanged)
                       name:@"GKPlayerAuthenticationDidChangeNotificationName"
                     object:nil];
        }
        // 玩家未登录认证,发起认证请求
        [localplayer authenticateWithCompletionHandler:nil];
        NSLog(@"登录中...");
    }
    // 释放使用的对象
    [localplayer release];
}
 
// 停止监听登录游戏状态变化
- (void)logoutGamecenter
{
    // 取消监听用户登录状态变化
    if(nc){
        [nc removeObserver:self
                      name:@"GKPlayerAuthenticationDidChangeNotificationName"
                    object:nil];
    }
}
*/
 
// H5 plus事件处理
var GKLocalPlayer=null,NSNotificationCenter=null;
var nc=null,delegate=null;
function plusReady(){
    plus.webview.currentWebview().setStyle({popGesture:"none"});
    if ( plus.os.name == "iOS" ) {
        GKLocalPlayer  = plus.ios.importClass("GKLocalPlayer");
        NSNotificationCenter = plus.ios.importClass("NSNotificationCenter");
        longinGamecenter();
    } else {
        eStart.innerText = "欢迎您";
        bLogin = true;
        setTimeout( function(){
            plus.nativeUI.toast( "此平台不支持Game Center功能!" );
        }, 500 );
    }
}
if(window.plus){
    plusReady();
}else{
    document.addEventListener("plusready",plusReady,false);
}
// DOMContentLoaded事件处理
var eStart=null,bLogin=false;
document.addEventListener("DOMContentLoaded",function(){
    eStart = document.getElementById( "start" );
},false);
 
// 游戏玩家登录状态监听函数
function authenticationChanged( notification ){
    // 获取游戏玩家共享实例对象
    var player = notification.plusGetAttribute("object");
    if ( player.plusGetAttribute("isAuthenticated") ) {
        // 玩家已登录认证,获取玩家信息
        playerInformation(player);
        bLogin = true;
    } else {
        // 玩家未登录认证,提示用户登录
        setTimeout(function(){
            eStart.innerText = "请登录";
            bLogin = false;
        },500);
    }
    // 释放使用的对象
//    plus.ios.deleteObject(player);
}
 
// 获取游戏玩家状态信息
function playerInformation( player ){
    var name = player.plusGetAttribute("displayName");
    eStart.innerText = name+" 已登录!";
}
 
// 登录到游戏中心
function longinGamecenter(){
    if ( bLogin ){
        return;
    }
    // 获取游戏玩家共享实例对象
    var localplayer = GKLocalPlayer.localPlayer();
    // 判断游戏玩家是否已经登录认证
    if ( localplayer.plusGetAttribute("isAuthenticated") ) {
        // 玩家已登录认证,获取玩家信息
        playerInformation( localplayer );
        bLogin = true;
    } else {
        eStart.innerText = "登录中...";
        // 监听用户登录状态变更事件
        if(!nc){
            nc = NSNotificationCenter.defaultCenter();
            delegate||(delegate=plus.ios.implements("NSObject",{"authenticationChanged:":authenticationChanged}));
            nc.addObserverselectornameobject(delegate,
                plus.ios.newObject("@selector","authenticationChanged:"),
                "GKPlayerAuthenticationDidChangeNotificationName",
                null);
        }
        // 玩家未登录认证,发起认证请求
        localplayer.authenticateWithCompletionHandler(null);
    }
    // 释放使用的对象
    plus.ios.deleteObject(localplayer);
}
 
// 停止监听登录游戏状态变化
function stopGamecenterObserver()
{
    // 取消监听用户登录状态变化
    nc&&nc.removeObservernameobject(delegate,"GKPlayerAuthenticationDidChangeNotificationName",null);
    plus.ios.deleteObject(nc);nc = null;
    plus.ios.deleteObject(delegate);delegate = null;
}
        </script>
        <style type="text/css">
* {
    -webkit-user-select: none;
    -ms-touch-select: none;
}
html {
    width: 100%;
    height: 100%;
}
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    -webkit-touch-callout:none;
    -webkit-tap-highlight-color:rgba(0,0,0,0);
}
header {
    height: 44px;
    font-size: 22px;
    line-height: 44px;
    font-weight: bold;
    color: #000000;
}
.game {
    position:fixed;
    top:44px;
    width:100%;
    bottom: 60px;
    background: no-repeat center center url(../img/5.jpg);
}
.button {
    font-size: 18px;
    font-weight: normal;
    text-decoration: none;
    display: block;
    text-align: center;
    color: #fff;
    background-color: #FFCC33;
    border: 1px solid #ECB100;
    padding: .5em 0em;
    margin: .5em .7em;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
.button:active {
    outline: 0;
    -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
    box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
.start {
    width: 50%;
    font-size: 18px;
    font-weight: normal;
    text-decoration: none;
    display: block;
    text-align: center;
    color: #666;
    background-color: #EBEBEB;
    border: 1px solid #E0E0E0;
    padding: .5em 0em;
    margin: .5em auto;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
        </style>
        <style type="text/css" media="screen and (min-aspect-ratio: 1/1)">
.autobg {
    background-size: auto 100%;
}
        </style>
        <style type="text/css" media="screen and (max-aspect-ratio: 1/1)">
.autobg {
    background-size: 100% auto;
}
        </style>
    </head>
    <body style="background:#CCCCCC;">
        <header id=header>五子棋</header>
        <div class="game autobg">
        </div>
        <div style="position:fixed;bottom:0;width:100%;height:60px;text-align:center;">
            <div class="button" onclick="try{stopGamecenterObserver();}catch(e){}back();">退出</div>
        </div>
        <div style="position:fixed;top:30%;width:100%;text-align:center;">
            <div id="start" class="start" onclick="longinGamecenter();">登录中...</div>
        </div>
    </body>
</html>