业务场景
当用户打开应用时,系统发送Push消息给用户,向用户推荐博主信息。用户关注博主后,将保存用户的订阅信息,当关注的博主更新内容时,向订阅的用户发送短信,提醒用户及时查看最新内容。从而持续增强了用户的互动性。
向用户发送Push消息
1.在应用中集成推送服务。
2.接收透传的Push消息。
3.在AndroidManifest.xml配置PushService,用于接收通知消息。
<service
android:name=".ui.PushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="push_kit_auto_init_enabled"
android:value="true" /
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
4. PushService继承自HmsMessageService,处理接收到的消息后, 使用自定义通知显示消息。
private void processWithin10s(RemoteMessage message) {
if (!TextUtils.isEmpty(message.getData())) {
JSONObject object = new JSONObject(message.getData());
String title = object.optString("title") + "";
String content = object.optString("content") + "";
String articleId = object.optString("articleId") + "";
String authorId = object.optString("authorId") + "";
NotificationUtils.sendNotify(this, title, content, articleId, authorId);
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
使用云数据库存储信息
1.开通云数据库服务。
2.新增和导出对象类型。新增Author、Article、SubscriptionRecord表,具体字段请下载Demo参考对应类名的实体类。
3.新增存储区。
4.集成云数据库SDK。
5.初始化云数据库。
public static void initAGConnectCloudDB(Context context) {
AGConnectCloudDB.AGConnectCloudDB.initialize(context);
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
6.调用getInstance(AGConnectInstance connectInstance, AGConnectAuth auth)方法获取对应数据处理位置的AGConnectCloudDB实例,并使用createObjectType创建对象类型。
mCloudDB = AGConnectCloudDB.getInstance(AGConnectInstance.getInstance(), AGConnectAuth.getInstance());
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
7.创建Cloud DB zone配置对象,并打开该Cloud DB zone(以Cloud DB zone的同步属性为缓存模式、访问属性为公共存储区为例),详细请参考CloudDBZoneConfig。
public void openCloudDBZoneV2() {
mConfig = new CloudDBZoneConfig(ZONE_NAME,CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
Task<CloudDBZone> openDBZoneTask = mCloudDB.openCloudDBZone2(mConfig, true);
openDBZoneTask.addOnSuccessListener(cloudDBZone -> {
mCloudDBZone = cloudDBZone;
addSubscription();
}).addOnFailureListener(e -> {
callBack.onPhotoErrorMessage("OpenCloudDBZone Failed: " + e.getMessage());
});
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
8.插入更新数据,用于存储新增或者修改的文章信息。
public void upsertArticle(Article article) {
Task<Integer> upsertTask = mCloudDBZone.executeUpsert(article);
upsertTask.addOnSuccessListener(cloudDBZoneResult -> {
ArrayList<Article> articles = new ArrayList<>();
articles.add(article);
callBack.onUpdate(articles);
}).addOnFailureListener(e -> callBack.onPhotoErrorMessage("upsert Error: " + e.getMessage()));
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
9.查询数据,用于查询所有文章信息。
public void queryAll() {
Task<CloudDBZoneSnapshot<Article>> queryTask = mCloudDBZone.executeQuery(
CloudDBZoneQuery.where(Article.class),
CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY);
queryTask.addOnSuccessListener(snapshot -> {
processQueryResult(snapshot);
}).addOnFailureListener(e -> {
callBack.onPhotoErrorMessage("queryAll Error: " + e.getMessage());
});
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084630869-994863088.gif)
触发记录用户的订阅信息
1.开通云函数服务。
2.本地编辑和打包操作云数据库的相关函数。
a.项目中引入AGC云数据库相关模块,并初始化认证信息。
const agconnect = require('@agconnect/common-server');
const clouddb = require('@agconnect/database-server/dist/index.js');
const SubscriptionRecord = require('./model/SubscriptionRecord.js');
let api_client_name = "./model/agc-apiclient-test.json";
let path = require('path');
let api_client_path = path.join(__dirname, api_client_name);
agconnect.AGCClient.initialize(agconnect.CredentialParser.toCredential(api_client_path));
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
let body = JSON.parse(event.body);//接收http触发器请求参数
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
b.初始化云数据库
try {
const agcClient = agconnect.AGCClient.getInstance();
clouddb.AGConnectCloudDB.initialize(agcClient);
const agconnectCloudDB = clouddb.AGConnectCloudDB.getInstance(agcClient);
const cloudDBZoneConfig = new clouddb.CloudDBZoneConfig('ArticleZone');
cloudDBZoneClient = agconnectCloudDB.openCloudDBZone(cloudDBZoneConfig);
let result = { "message": "openCloudDBZone success" };
logger.info(result);
} catch (error) {
let result = { "initDB message": error.message };
logger.info(result);
callback(result);
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
c.插入用户订阅信息到云数据库,用于存储用户订阅信息。
async function upsertSubscriptionRecord(SubscriptionRecord, callback, logger) {
if (!cloudDBZoneClient) {
console.log("CloudDBClient is null, try re-initialize it");
let result = { "message": "CloudDBClient is null" };
logger.info(result);
return;
}
try {
resp = await cloudDBZoneClient.executeUpsert(SubscriptionRecord);
console.log('The number of upsert record is:', resp);
let result = { "message": "upsertSubscriptionRecord success " };
logger.info(result);
} catch (error) {
let result = { "upsert err ": error.message };
logger.info(result);
callback(result);
}
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
d.查询订阅列表,用于判断用户是否订阅作者。
async function querySubscriptionRecordWithOrder(authorId, phone, callback, logger) {
try {
const cloudDBZoneQuery = clouddb.CloudDBZoneQuery.where(SubscriptionRecord.SubscriptionRecord).equalTo("authorId", parseInt(authorId)).equalTo("phone", phone);
const resp = await cloudDBZoneClient.executeQuery(cloudDBZoneQuery);
console.log('The number of query records is:', resp.getSnapshotObjects().length);
isSubscription = resp.getSnapshotObjects().length > 0 ? true : false;
if (isSubscription) {
// await sendSMS(phone);
}
} catch (error) {
let result = { "query err ": error.message };
logger.info(result);
callback(result);
}
}
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
3.创建云函数,将上一步打包的函数上传到该云函数中。
4.在上步创建的云函数中创建HTTP触发器。
5.在应用客户端集成云函数SDK。
6.初始化云函数,调用HTTP触发器获取云数据库相关操作结果。
function = AGConnectFunction.getInstance();
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
HashMap<String, String> map = new HashMap();
map.put("type", "1");
map.put("articleId", msg.getArticleId());
map.put("authorId", msg.getAuthorId());
map.put("phone", MainApplication.phone);
map.put("uid", MainApplication.uid);
function.wrap("insert-article-$latest").call(map)
.addOnCompleteListener(new OnCompleteListener<FunctionResult>() {
@Override
public void onComplete(Task<FunctionResult> task) {
if (task.isSuccessful()) {
String value = task.getResult().getValue();
JSONObject object = new JSONObject(value);
String message = (String) object.get("message");
//云函数返回成功
} }
});
![](/i/l/?n=22&i=blog/1969374/202209/1969374-20220908084642805-2077759653.gif)
内容变更向关注用户发送短信
1.开通短信服务。
测试功能
1.应用中新发布一个文章。
2.在AGC推送服务页面新增一条推送消息,推送给测试手机,手机是否收到消息提示用户有新内容发布,点击通知栏,是否可以跳转至详情页。
3.在详情页关注作者,检查数据库中是否已经存入相关信息。
4.重新发布一个文章,检查是否收到更新内容的推送消息。
下载Demo
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
标签:ACG,用户,专栏,result,博主,let,message,logger,const From: https://www.cnblogs.com/developer-huawei/p/16668243.html