gpt4 book ai didi

java - Android/Java : how to send notifications to user,,即使应用程序未使用 "actively"?

转载 作者:行者123 更新时间:2023-12-02 01:44:53 25 4
gpt4 key购买 nike

我希望能够在发生变化时向用户发送通知。例如,我的应用程序与犯罪相关。因此,用户可以提交其社区发生的犯罪报告。

当报告新的犯罪事件时,我希望能够向该特定社区的所有用户发送通知,即使他们没有积极使用该应用。

如何做到这一点?我对此很陌生,但据我了解,Firebase Messaging 等服务要求您手动输入消息并选择要手动发送消息的用户。我想知道是否有一种方法可以在不需要有人手动工作的情况下完成此操作?

类似于 snapchat/instagram 之类的东西会向您发送通知,表明有人向您发送了消息,即使您没有使用手机。

就我而言,我只想显示相同的标准通知“您所在地区的新犯罪”...

我该怎么做? (目前对于通知,我仅使用通知 channel ),非常感谢!

最佳答案

您可以通过 FCM 集成使用 Parse Server 轻松完成此操作。

首先,您需要设置 Android 应用才能接收推送通知

只需按照此快速入门操作即可:https://docs.parseplatform.org/parse-server/guide/#push-notifications-quick-start

其次,需要创建云代码函数

我建议您创建一个云代码函数,该函数将接收邻居作为参数,查询该邻居中的用户安装并向所有用户发送推送通知。

它会是这样的:

Parse.Cloud.define('notifyCrime', async req => {
const query = new Parse.Query(Parse.Installation);
query.equalTo('neighborhood', req.params.neighborhood); // I'm supposing you have a field called neighborhood in your installation class - if not, you can save this field there when the user sign up
await Parse.Push.send({
where: query,
data: {
alert: 'There is a crime in your neighborhood'
},
useMasterKey: true
});
});

引用:https://docs.parseplatform.org/js/guide/#sending-pushes-to-queries

第三,您需要从Android应用程序调用云函数

一旦某个用户举报犯罪,您可以调用您在步骤 2 中创建的云代码函数来通知同一社区中的所有其他用户。

它会是这样的:

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("neighborhood", "The neighborhood goes here");
ParseCloud.callFunctionInBackground("notifyCrime", params, new FunctionCallback<Object>() {
void done(Object response, ParseException e) {
if (e == null) {
// The users were successfully notified
}
}
});

引用:https://docs.parseplatform.org/cloudcode/guide/#cloud-functions

关于java - Android/Java : how to send notifications to user,,即使应用程序未使用 "actively"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57466076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com