优量汇视频成功服务端验证


优量汇视频成功服务端验证

服务端核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
exports.video_ylh = async function (req, res) {
try {
var data = req.query;
if (data) {
// data.userid 就是客户端传的玩家id .setUserId(JsbService.playerId)
var result = await cashs.video_ylh(data);
}
res.json({ "isValid": true });
} catch (exp) {
res.json({ "isValid": false });
}
};

安卓端回调传玩家id核心代码:

1
2
3
4
5
6
7
        rewardVideoAD = new RewardVideoAD(this, GGW_ID, this, true);
ServerSideVerificationOptions options = new ServerSideVerificationOptions.Builder()
// .setCustomData("data") // 设置激励视频服务端验证的自定义信息
.setUserId(JsbService.playerId) // 设置服务端验证的用户信息
.build();
rewardVideoAD.setServerSideVerificationOptions(options);
rewardVideoAD.loadAD();

安卓端全部代码:

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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;

import android.Manifest;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.ads.rewardvideo.ServerSideVerificationOptions;
import com.qq.e.comm.managers.GDTADManager;
import com.qq.e.comm.util.AdError;
import com.umeng.analytics.MobclickAgent;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import java.util.Locale;
import java.util.Map;

public class AppActivity extends Cocos2dxActivity implements RewardVideoADListener{

public static boolean IS_DYNAMIC = false; // 是否权限全部获取
public static boolean isReward = false; // 是否触发奖励

private AppActivity m_activity;
private ClipboardManager m_clipboard;

// 网络监测
private static AppActivity app = null;
public static int netState = -1;
private static NetworkStateBroadcastReceiver broadcastReceiver = null;

//激励视频广告位appID
private static final String GGW_APPID = "xxxxx";

private static final String TAG = AppActivity.class.getSimpleName();

private static RewardVideoAD rewardVideoAD;

//激励视频广告地址
private static final String GGW_ID = "xxxxxxxxx";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Workaround in
// https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);

//初始化广告通sdk
GDTADManager.getInstance().initWith(this, GGW_APPID);

m_activity = this;
Log.e("Difas", " 初始化 Init ");
JsbService.init(this);


//启动先加载一个广告
loadAdvertisement();

initSDK();

initWiFi();
}

// 初始化网络监测
public void initWiFi() {
app = this;
app.broadcastReceiver = new NetworkStateBroadcastReceiver();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, filter);
}


// 初始化SDK
public void initSDK(){
WeixinService.getInstance().init(m_activity);
}

// 权限申请
public void getPermissionsDynamic(){
if (Build.VERSION.SDK_INT >= 23) {
//此处做动态权限申请
int READ_PHONE_STATE = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
int ACCESS_COARSE_LOCATION = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int ACCESS_FINE_LOCATION = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int WRITE_EXTERNAL_STORAGE = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//缺少权限,进行权限申请
if (READ_PHONE_STATE != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE},123);
} else if (ACCESS_COARSE_LOCATION != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},123);
} else if (ACCESS_FINE_LOCATION != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},123);
} else if (WRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},123);
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

getPermissionsDynamic();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
}

@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
SDKWrapper.getInstance().setGLSurfaceView(glSurfaceView, this);

if (Build.VERSION.SDK_INT >= 28) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
}

return glSurfaceView;
}

@Override
protected void onResume() {
super.onResume();
SDKWrapper.getInstance().onResume();
MobclickAgent.onResume(this);
}

@Override
protected void onPause() {
super.onPause();
SDKWrapper.getInstance().onPause();
MobclickAgent.onPause(this);
}

@Override
protected void onDestroy() {
super.onDestroy();

if (!isTaskRoot()) {
return;
}

SDKWrapper.getInstance().onDestroy();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
SDKWrapper.getInstance().onNewIntent(intent);
}

@Override
protected void onRestart() {
super.onRestart();
SDKWrapper.getInstance().onRestart();
}

@Override
protected void onStop() {
super.onStop();
SDKWrapper.getInstance().onStop();
}

@Override
public void onBackPressed() {
SDKWrapper.getInstance().onBackPressed();
super.onBackPressed();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
SDKWrapper.getInstance().onConfigurationChanged(newConfig);
super.onConfigurationChanged(newConfig);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
SDKWrapper.getInstance().onRestoreInstanceState(savedInstanceState);
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
SDKWrapper.getInstance().onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}

@Override
protected void onStart() {
SDKWrapper.getInstance().onStart();
super.onStart();
}


/*
* 向剪贴板中添加文本
*/
public void copyTextToClipboard(final String str){
try{
if (Looper.myLooper() == null){
Looper.prepare();
}
m_clipboard = (ClipboardManager) this.getSystemService(this.CLIPBOARD_SERVICE);
ClipData textCd = ClipData.newPlainText("data", str);
m_clipboard.setPrimaryClip(textCd);
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}catch (Exception e){
e.printStackTrace();
}
}

public boolean isMainProcess() {
int pid = android.os.Process.myPid();
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return getApplicationInfo().packageName.equals(appProcess.processName);
}
}
return false;
}

/***************************************************优量汇广告start***************************************************/
private boolean adLoaded = false;
// 1.加载广告,先设置加载上下文环境和条件
public void loadAdvertisement() {
adLoaded = false;
rewardVideoAD = new RewardVideoAD(this, GGW_ID, this, true);
ServerSideVerificationOptions options = new ServerSideVerificationOptions.Builder()
// .setCustomData("data") // 设置激励视频服务端验证的自定义信息
.setUserId(JsbService.playerId) // 设置服务端验证的用户信息
.build();
rewardVideoAD.setServerSideVerificationOptions(options);
rewardVideoAD.loadAD();
}

// 播放广告
public void playAdvertisement(){
if (adLoaded) {
if (rewardVideoAD != null && !rewardVideoAD.hasShown()) {
long delta = 1000;
if (SystemClock.elapsedRealtime() < (rewardVideoAD.getExpireTimestamp() - delta)) {
Log.i("Difas", "视频素材缓存成功");
isReward = false;
rewardVideoAD.showAD();
} else {
// Toast.makeText(this, "激励视频广告已过期,请再次请求广告后进行广告展示!", Toast.LENGTH_LONG).show();
loadAdvertisement();
}
} else {
// Toast.makeText(this, "此条广告已经展示过,请再次请求广告后进行广告展示!", Toast.LENGTH_LONG).show();
loadAdvertisement();
}
} else {
// Toast.makeText(this, "成功加载广告后再进行广告展示!", Toast.LENGTH_LONG).show();
loadAdvertisement();
}
}

/**
* 广告加载成功,可在此回调后进行广告展示
**/
@Override
public void onADLoad() {
adLoaded = true;
}

/**
* 视频素材缓存成功,可在此回调后进行广告展示
*/
@Override
public void onVideoCached() {
adLoaded = true;
}

/**
* 激励视频广告页面展示
*/
@Override
public void onADShow() {
}

/**
* 激励视频广告曝光
*/
@Override
public void onADExpose() {
}

/**
* 激励视频触发激励(观看视频大于一定时长或者视频播放完毕)
*/
@Override
public void onReward(Map<String, Object> map) {
isReward = true;
}

/**
* 激励视频广告被点击
*/
@Override
public void onADClick() {
}

/**
* 激励视频播放完毕
*/
@Override
public void onVideoComplete() {
//播放之后加载下一个广告
loadAdvertisement();
}

/**
* 激励视频广告被关闭
*/
@Override
public void onADClose() {
if (isReward) { // 是否激活奖励
m_activity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("game.event.dispatchEvent(game.event.JSB_AD_VIDEO_CB, {success:true})");
}
});
} else {
m_activity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("game.event.dispatchEvent(game.event.JSB_AD_VIDEO_CB, {success:fales})");
}
});
}
}

/**
* 广告流程出错
*/
@Override
public void onError(AdError adError) {
Log.i("Difas", "广告流程出错");

loadAdvertisement();

}

/***************************************************优量汇广告 end ***************************************************/

}