I am using Amazon Push Notifications in my Spring MVC 4 project. I have used CronTrigger to send push notifications to Android app everyday at 8am. I have also used Timezone along with CronTrigger so that users get notifications according to their respective timezones.
Here is my WebConfig.java:
@Configuration
@EnableScheduling
@EnableWebMvc
@ComponentScan(basePackages="com.project")
public class WebConfig implements SchedulingConfigurer
{
protected static final Logger slf4jLogger = Logger.getLogger(WebConfig.class.getName());
private static final String cronExpression = "0 8 * * * ?";
/*@Bean
public MobileNotifSchedulerBean schedulerbean()
{
return new MobileNotifSchedulerBean();
}*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setSuffix(".html");
resolver.setSuffix(".htm");
return resolver;
}
@Bean
CronTrigger cronTrigger()
{
//The code in FetchUserTimeZones.java fetches all the user timezones which are stored in DynamoDb. Eg timeZone = "Asia/Calcutta";
String timeZone = null;
HashSet<String> userTimeZonesfromDB = FetchUserTimeZones.fetchUserTimeZone();
for (String s : userTimeZonesfromDB)
{
timeZone = s;
slf4jLogger.info(s);
}
return new CronTrigger(cronExpression, TimeZone.getTimeZone(timeZone));
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar)
{
taskRegistrar.addCronTask(new CronTask(new MobileNotifSchedulerBean(), cronTrigger()));
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor()
{
return Executors.newScheduledThreadPool(1);
}
}
Here is my MobileNotifSchedulerBean: This code fetches a random question from DynamoDb and send them in Push Notification for each GCM registrationID with help of the CronTrigger set to time (8am). I have used snsmobilepush.zip from http://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html.
@EnableScheduling
public class MobileNotifSchedulerBean implements Runnable
{
protected static final Logger slf4jLogger = Logger.getLogger(MobileNotifSchedulerBean.class.getName());
public MobileNotifSchedulerBean()
{
run();
}
public void sendQuestionNotif()
{
try
{
HashSet<String> reg_ids = FetchRegistrationIDs.fetchItems();
for (String s : reg_ids)
{
String REGISTRATION_IDs = s;
slf4jLogger.info(s);
MobileSNSPushNotification.sendNotification(REGISTRATION_IDs);
}
}
catch (IOException e)
{
//e.printStackTrace();
slf4jLogger.error(e);
slf4jLogger.error(e.getMessage());
slf4jLogger.error(e.getStackTrace());
}
}
@Override
public void run()
{
sendQuestionNotif();
}
}
Please help me where I have went wrong. Only problem is push notifications are going in wrong times and also multiple number of notifications instead of going only 1 push notification per user, per day(8am). TIA.
Update: There was a correction in Cron Expression. I corrected it.
private static final String cronExpression = "0 0 8 * * ?"; // For everyday 8 am.
But still problem hasn't fixed
You should create a separate schedule for each user