Android/Java:如何向用户发送通知,即使没有 "actively" 使用应用程序?

Android/Java: how to send notifications to user, even when app is not "actively" being used?

我希望能够在发生变化时向用户发送通知。 例如,我的申请与犯罪有关。这样用户就可以提交他们社区发生的犯罪报告。

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

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

类似于 snapchat/instagram 和东西会向您发送有人向您发送消息的通知,即使您没有使用 phone。

就我而言,我只想显示相同的标准通知 "New crime in your area"...

我该怎么做? (目前我只是使用通知渠道通知),非常感谢!

"my understanding services like Firebase Messaging require you to type out a message manually and select users to send the message to manually".

这不完全正确。有一个名为 Firebase Topic Messaging 的方法,可让您仅向特定用户群发送通知。您必须从该应用程序注册该主题,然后您可以根据用户订阅的主题向您的用户组发送自定义消息。

您可以通过 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