Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send Push Notification from Server Side Java automatically 3rd month between start date and end date for each different user?

I know this is a simple question but I am new here. Can I know whether there is any option to send push notification from google cloud(backend, java server) to devices(Android) every 3rd month till end date. If so how?? and can I trigger repeating notification for an interval of time??

In my android app(client) I have these classes https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart

then I made a backend in java and I have these classes there:

public class RegisterUserDetails {

public String RegisterUserDetails(String  data) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity fdUsers = new Entity("fdUser");
    Logger log = Logger.getLogger(RegisterUserDetails.class.getName());
    log.setLevel(Level.INFO);
    JsonElement jsonElement = new JsonParser().parse(data);
    JsonArray jsonArray = jsonElement.getAsJsonArray();
   // JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();

    String regId = jsonArray.get(0).getAsString();
    String userName = jsonArray.get(1).getAsString();

    log.info("regId"+regId);
    log.info("userName"+userName);

    fdUsers.setProperty("regId", ""+regId);
    fdUsers.setProperty("userName", ""+userName);
 //   fdUsers.setProperty("userName", "" + userName);

    datastore.put(fdUsers);
    return "Success";
}
}

and I have

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiNamespace;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.inject.Named;

import static com.tiya.accountbook.backend.OfyService.ofy;

/**
 * An endpoint to send messages to devices registered with the backend
 * <p/>
 * For more information, see
 * https://developers.google.com/appengine/docs/java/endpoints/
 * <p/>
 * NOTE: This endpoint does not use any form of authorization or
 * authentication! If this app is deployed, anyone can access this endpoint! If
 * you'd like to add authentication, take a look at the documentation.
 */
@Api(
        name = "messaging",
        version = "v1",
        namespace = @ApiNamespace(
                ownerDomain = "backend.accountbook.tiya.com",
                ownerName = "backend.accountbook.tiya.com",
                packagePath = ""
        )
)
public class MessagingEndpoint {
    private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName());

    /**
     * Api Keys can be obtained from the google cloud console
     */
    private static final String API_KEY = System.getProperty("gcm.api.key");

    /**
     * Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
     *
     * @param message The message to send
     */
    public void sendMessage(@Named("message") String message) throws IOException {
        if (message == null || message.trim().length() == 0) {
            log.warning("Not sending message because it is empty");
            return;
        }
        // crop longer messages
        if (message.length() > 1000) {
            message = message.substring(0, 1000) + "[...]";
        }
        Sender sender = new Sender(API_KEY);
        Message msg = new Message.Builder().addData("message", message).build();
        List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list();
        for (RegistrationRecord record : records) {
            Result result = sender.send(msg, record.getRegId(), 5);
            if (result.getMessageId() != null) {
                log.info("Message sent to " + record.getRegId());
                String canonicalRegId = result.getCanonicalRegistrationId();
                if (canonicalRegId != null) {
                    // if the regId changed, we have to update the datastore
                    log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
                    record.setRegId(canonicalRegId);
                    ofy().save().entity(record).now();
                }
            } else {
                String error = result.getErrorCodeName();
                if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                    log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
                    // if the device is no longer registered with Gcm, remove it from the datastore
                    ofy().delete().entity(record).now();
                } else {
                    log.warning("Error when sending message : " + error);
                }
            }
        }
    }
}

then what to do??

When a user save an account detail he will save a start date and an end date to the google cloud. I want to send notification to this user every 3rd month from this start date till the end date. Like this there will be many accounts it can be from same user or different. I want to know whether this is possible??? If so how??? –

like image 812
Elizabeth Avatar asked Mar 29 '16 07:03

Elizabeth


3 Answers

Please use Quartz scheduler and schedule a job to run a particular code every month. You can find the tutorial here: http://www.mkyong.com/java/quartz-scheduler-example/

like image 122
Alok Gupta Avatar answered Oct 16 '22 23:10

Alok Gupta


If you wanted to send a specific message to all users every month (e.g. on the 1st of the month message all users), you could use a cron job that runs every month.

As documented here: https://cloud.google.com/appengine/docs/java/config/cron

As per the docs, you could say

2nd,third mon,wed,thu of march 17:00
1st monday of sep,oct,nov 17:00
1 of jan,april,july,oct 00:00
like image 43
TStu Avatar answered Oct 17 '22 00:10

TStu


You can look at Azure Notifications Hub. https://azure.microsoft.com/en-in/pricing/details/notification-hubs/

Its top offering supports scheduled push messages. Though it is a bit pricey.

Instead, simple logic on server side can also be written to handle recurrence and automatically add message to outgoing push message queue. Hardly a couple of days work.

like image 27
Abhijit Ajmera Avatar answered Oct 17 '22 00:10

Abhijit Ajmera