我的目标
激活我的开关后,每8点接收一次通知
当前结果
-如果我第一次激活该开关,每次启动该应用程序时都会收到通知
-我也随时收到通知,而没有启动应用程序
-此外,我会在一天内收到几条通知
我做了什么(我的代码)
-NotificationActivity(onCreate的一部分)
private void onClickSwitch() {
aSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
retrieveKeyWord();
retrieveCategories();
keywordPref = mPreferences.getString(PREF_KEYWORD, null);
categoriesPref = mPreferences.getString(PREF_CATEGORIES, null);
mPreferences.edit().putString(PREF_KEYWORD, keyword).apply();
mPreferences.edit().putString(PREF_CATEGORIES, categories).apply();
Calendar currentDate = Calendar.getInstance();
Calendar dueDate = Calendar.getInstance();
dueDate.set(Calendar.HOUR_OF_DAY, 8);
dueDate.set(Calendar.MINUTE, 0);
dueDate.set(Calendar.SECOND, 0);
if(dueDate.before(currentDate)) {
dueDate.add(Calendar.HOUR_OF_DAY, 24);
}
long timeDiff = dueDate.getTimeInMillis() - currentDate.getTimeInMillis();
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).setInitialDelay(timeDiff, TimeUnit.MILLISECONDS)
.setConstraints(constraints)
.addTag("TAG_OUTPUT").build();
WorkManager.getInstance().enqueue(oneTimeWorkRequest);
}
});
}
-NotificationActivity->布局
-NotificationWorker
public class NotificationWorker extends Worker {
private SharedPreferences mPreference;
private String keyword;
private String categories;
private ResultModel resultModel;
private ResultContract.Model.OnFinishedListener onFinishedListener;
private int numberOfArticle;
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
mPreference = getApplicationContext().getSharedPreferences(NotificationActivity.PREFERENCE_FILE, MODE_PRIVATE);
keyword = mPreference.getString(NotificationActivity.PREF_KEYWORD, null);
categories = mPreference.getString(NotificationActivity.PREF_CATEGORIES, null);
resultModel = new ResultModel();
numberOfArticle = resultModel.getResultListWithoutDate(onFinishedListener, 1, keyword, categories);
displayNotification(keyword);
return Result.success();
}
private void displayNotification(String keyword) {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("simplifiedcoding", "simplifiedcoding", NotificationManager.IMPORTANCE_DEFAULT);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "simplifiedcoding")
.setContentTitle("Articles sur : " + keyword)
.setContentText("Nombre d'articles trouvés : " + numberOfArticle)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher_background);
assert notificationManager != null;
notificationManager.notify(1, builder.build());
}
}
如果需要更多信息,请告诉我,感谢谁会帮助我
您可以通过3种方法解决它。
第一:
[用户警报管理器,并把它放在IF中,所以检查活动仍在进行中,因此,如果您的应用已打开,它将起作用。您可以从下面的Google文档链接中获取更多信息:
第二:
您可以使用带有任务的计时器来创建通知,您可以检查活动是否再次存在,但是更好的用法是在前台服务上创建计时器。
第三:
甚至在您需要它时,也可以从服务器使用PushNotification。文档和信息(如果在下面链接):
Push Notification - One Signal
注意:
我建议您使用第三种方式解决问题。
具有良好的编码,别忘了给我投票;)