【金山文档】 习题
https://kdocs.cn/l/sghlHpDYX1pR
【金山文档】 push
https://kdocs.cn/l/scLIh2SC0vy0
PushGetTokenActivity.java
package com.yy.yy002;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
public class PushGetTokenActivity extends Activity implements View.OnClickListener {
private static final String TAG = "PushGetTokenActivity";
private TextView mtokenInforView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_push_get_token);
mtokenInforView = findViewById(R.id.tokeninfo);
findViewById(R.id.btn_get_token).setOnClickListener(this);
findViewById(R.id.btn_delete_token).setOnClickListener(this);
getIntentData(getIntent());
}
@Override
public void onClick(View view) {
int viewId = view.getId();
switch (viewId) {
case R.id.btn_get_token:
getToken();
break;
case R.id.btn_delete_token:
deleteToken();
break;
default:
break;
}
}
private void getToken() {
new Thread() {
@Override
public void run() {
try {
// read from agconnect-services.json
String appId = AGConnectServicesConfig.fromContext(getApplicationContext()).getString("client/app_id");
String token = HmsInstanceId.getInstance(getApplicationContext()).getToken(appId, "HCM");
Log.i(TAG, "get token:" + token);
// todo 1.ing the getToken method, null judgment must be performed.
//todo 对于getToken方法,必须执行null判断
// todo 2. In the outer code segme After a token is obtained by usnt of the getToken method, you need to add exception capture processing.
//todo 在外部代码段中,在getToken方法的usnt获得一个令牌之后,需要添加异常捕获处理。
if (!TextUtils.isEmpty(token)) {
sendRegTokenToServer(token);
}
mtokenInforView.append("\n" +"get token:" + token);
} catch (ApiException e) {
Log.e(TAG, "get token failed, " + e);
mtokenInforView.append("\n" +"get token failed, " + e);
}
}
}.start();
}
private void sendRegTokenToServer(String token) {
Log.i(TAG, "sending token to server. token:" + token);
}
private void deleteToken() {
new Thread() {
@Override
public void run() {
try {
// read from agconnect-services.json
String appId = AGConnectServicesConfig.fromContext(PushGetTokenActivity.this).getString("client/app_id");
HmsInstanceId.getInstance(PushGetTokenActivity.this).deleteToken(appId, "HCM");
Log.i(TAG, "deleteToken success.");
mtokenInforView.append("\n" + "deleteToken success");
} catch (ApiException e) {
Log.e(TAG, "deleteToken failed." + e);
mtokenInforView.append("\n" + "deleteToken failed." + e);
}
}
}.start();
}
private void getIntentData(Intent intent) {
if (intent != null) {
// todo You can use the following three lines of code to obtain the values for dotting statistics:
//todo 您可以使用以下三行代码来获取点统计数据的值
String msgid = intent.getStringExtra("_push_msgid");
String cmdType = intent.getStringExtra("_push_cmd_type");
int notifyId = intent.getIntExtra("_push_notifyid", -1);
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
String content = bundle.getString(key);
Log.i(TAG, "receive data from push, key = " + key + ", content = " + content);
}
}
Log.i(TAG, "receive data from push, msgId = " + msgid + ", cmd = " + cmdType + ", notifyId = " + notifyId);
} else {
Log.i(TAG, "intent is null");
}
}
}
PushMsgService.java
/*
* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
* This subclass is created by HMS Core Toolkit
* and used to receive token information or messages returned by HMS server
*
*/
package com.yy.yy002;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hms.push.HmsMessageService;
public class PushMsgService extends HmsMessageService {
private static final String TAG = "PushService";
// This method callback must be completed in 10 seconds. Otherwise, you need to start a new Job for callback processing.
// extends HmsMessageService super class
@Override
public void onNewToken(String token) {
Log.i(TAG, "received refresh token:" + token);
// send the token to your app server.
if (!TextUtils.isEmpty(token)) {
refreshedTokenToServer(token);
}
}
private void refreshedTokenToServer(String token) {
Log.i(TAG, "sending token to server. token:" + token);
}
}