如果 firebase 数据库有任何更改,我想向用户发送通知。我试过使用Broadcast-Receiver, Service, Work-Manager, Alarm-Manager. 但它们都不是完美的。它仅在应用程序正在运行或在后台运行并且在应用程序关闭时也可以在某些设备中工作[在 android 7 中测试] 。但是在 android 10 [在三星和小米测试] 中,应用程序关闭时它不起作用。当我在小米的应用程序中关闭电池保护程序时,它会在关闭应用程序后的某个时间工作,然后停止工作。有人说,你有turn-off battery saverandturn-on auto-start mode in Xiaomi和do something in other manufacturers phone。但是有很多应用程序显示通知,即使我已经超过 10/15 天没有打开应用程序并且我已经检查了那些省电模式和自动启动关闭的应用程序的应用程序信息[在 Xiaomi-android10 中检查]。我究竟做错了什么?服务:
public class ServiceManager extends Service {
public ServiceManager() {}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
getLastModifiedTime2(new HomePage.MyCallback() {
@Override
public void onCallback(String roll) {
SharedPreferences spExtra = getSharedPreferences("spExtra",MODE_PRIVATE);
if(!Objects.equals(spExtra.getString("1", "No changes"), roll)){
SharedPreferences.Editor editor = spExtra.edit();
editor.putString("1",roll);
editor.apply();
showNotification(getApplicationContext());
//
}
}
});
}
@Override
public void onDestroy() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startid) {
System.out.println("called start command");
//
getLastModifiedTime2(new HomePage.MyCallback() {
@Override
public void onCallback(String roll) {
SharedPreferences spExtra = getSharedPreferences("spExtra",MODE_PRIVATE);
if(!Objects.equals(spExtra.getString("1", "No changes"), roll)){
SharedPreferences.Editor editor = spExtra.edit();
editor.putString("1",roll);
editor.apply();
showNotification(getApplicationContext());
}
}
});
//
return START_STICKY;
}
private void stopService() {
}
//not showing the method getlastmodifiedtime2 and shownotification;
}
并开始以下服务:
startService(new Intent(this, ServiceManager.class));
工作经理:
public class UploadWorker extends Worker {
public UploadWorker(
@NonNull Context context,
@NonNull WorkerParameters workerParams){
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
System.out.println("UploadWorker called");
getLastModifiedTime2(new HomePage.MyCallback() {
@Override
public void onCallback(String roll) {
SharedPreferences spExtra = getApplicationContext().getSharedPreferences("spExtra",MODE_PRIVATE);
if(!Objects.equals(spExtra.getString("1", "No changes"), roll)){
SharedPreferences.Editor editor = spExtra.edit();
editor.putString("1",roll);
editor.apply();
showNotification(getApplicationContext());
//
}
//startNewRequest();
}
});
return Result.success();
//return Result.Retry.retry();
}
//not showing function getlastmodifiedtime2 and shownotification
}
如下调用工人:
PeriodicWorkRequest workRequest = new PeriodicWorkRequest
.Builder(UploadWorker.class,15,TimeUnit.MINUTES).build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork("firebaseTest",
ExistingPeriodicWorkPolicy.KEEP,workRequest);
和警报管理器代码:
Calendar cal = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 25);
calendar.set(Calendar.SECOND, 30);
Intent intent = new Intent(HomePage.this, ServiceManager.class);
PendingIntent pintent = PendingIntent.getService(HomePage.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
System.out.println("called alarm-intent");
System.out.println("called"+cal.getTimeInMillis());
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
当应用程序关闭时,它们都不起作用。有人说要使用前台服务,但它不会一直显示通知。我应该使用什么流程来完成我的工作[在 Firebase 中更改数据时通知用户]。