是否可以在每个不同用户的开始日期和结束日期之间的第3个月自动从服务器端Java发送推送通知?

Eli*_*eth 6 android push-notification google-cloud-messaging

我知道这是一个简单的问题,但我是新来的.我是否可以知道是否有任何选项可以每隔3个月从谷歌云(后端,java服务器)向设备(Android)发送推送通知直到结束日期.如果是这样的话?我可以在一段时间内触发重复通知吗?

在我的Android应用程序(客户端)中,我有这些类https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart

然后我在java中创建了一个后端,我在那里有这些类:

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";
}
}
Run Code Online (Sandbox Code Playgroud)

我有

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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后该怎么办?

当用户保存帐户详细信息时,他会将开始日期和结束日期保存到Google云端.我想从开始日期到结束日期每隔3个月向该用户发送通知.像这样会有很多帐户可以来自同一个用户或不同.我想知道这是否可行??? 如果是这样的??? -

小智 2

请使用 Quartz 调度程序并安排一个作业每月运行特定的代码。您可以在这里找到教程:http://www.mkyong.com/java/quartz-scheduler-example/