简体   繁体   中英

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

I want to be able to send a notification to a user IF something changes. For example, my application is crime-related. So users can submit reports of crimes that have happened in their neighborhoods.

When a new crime is reported, I want to be able to send ALL users in that specific neighbourhood a notification, even if they are not actively using the app.

How can this be done? I'm quite new at this but to my understanding services like Firebase Messaging require you to type out a message manually and select users to send the message to manually. I'm wondering if there's a way this can be done without someone having to manually do work?

Similar to how snapchat/instagram and stuff will send you notifications that someone has sent you a message even when you are not using your phone.

In my case, I just want the same standard notification "New crime in your area" to be displayed...

How can I do this? (Currently for notifications I'm just using Notification Channels), thank you so much!

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

This is not completely true. There is a method name Firebase Topic Messaging , that lets you send notifications to specific user segments only. You have to register from the app for that topic and then, you can send customized message to your user groups based on topics they subscribed to.

You can easily do this using Parse Server through FCM integration.

First, you need to setup your Android app to be able to receive push notifications

Just follow this Quickstart: https://docs.parseplatform.org/parse-server/guide/#push-notifications-quick-start

Second, you need to create a cloud code function

I suggest you to create a cloud code function that will receive the neighborhood as parameter, will query for the user installations in that neighborhood and send the push notification to all of them.

It would be something like this:

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
  });
});

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

Third, you need to call the cloud function from your Android app

Once some user has reported a crime, you can call the cloud code function that you created in step 2 to notify all other users in the same neighborhood.

It would be something like this:

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
       }
   }
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM