如何解决在所有设备中关闭应用程序时,没有任何方法可以显示通知吗?
如果 firebase 数据库有任何更改,我想向用户发送通知。我曾尝试使用 Broadcast-Receiver
、Service
、Work-Manager
、Alarm-Manager
。但它们都不是完美的。它仅在应用程序正在运行或在后台运行时有效,并且在应用程序关闭时也可以在某些设备[在 android 7 中测试] 中运行。但是在android 10中[在三星和小米中测试],当应用程序关闭时它不起作用。当我在小米的应用程序中关闭省电模式时,它会在关闭应用程序后的某个时间工作,然后停止工作。有人说,你有turn-off battery saver
和turn-on auto-start mode
in Xiaomi
和do something
in other manufacturers phone
。但是即使我已经超过 10/15 天没有打开应用程序,也有许多应用程序显示通知并且我已经检查了那些应用程序的应用程序信息打开和自动启动关闭[在小米-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",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",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,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 中更改数据时通知用户]。
解决方法
您需要显示通知,请参阅此 link。
前台服务是一个单独的问题。在 Oreo+ 设备中,当应用处于后台或关闭时,服务无法运行除非服务在前台启动,在这种情况下,它们需要在服务运行的整个时间显示通知。
,当应用程序被杀死或关闭时,上面给出的所有这些都不起作用。然后我尝试了 Firebase cloud messaging
并且它工作[在 android 7,10 中测试],即使应用程序被关闭或杀死。
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(this);
--
public void sendNotificationToUser() {
JSONObject mainObj = new JSONObject();
try {
mainObj.put("to","/topics/" + topic_name);//
JSONObject notificationObj = new JSONObject();
notificationObj.put("title","About title");
notificationObj.put("body","Enter you message here");
mainObj.put("notification",notificationObj);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,value,mainObj,new Response.Listener<JSONObject>() {//value="https://fcm.googleapis.com/fcm/send"
@Override
public void onResponse(JSONObject response) {
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String,String> header = new HashMap<>();
header.put("content-type","application/json");
header.put("authorization","key=" + server-key);
return header;
}
};
Toast.makeText(this,"Successfully added",Toast.LENGTH_SHORT).show();
requestQueue.add(jsonObjectRequest);
//
}
}
确保导入
implementation 'com.mcxiaoke.volley:library:1.0.19'
不要将您的服务器密钥放在您的应用程序中。您可以使用 Firebase cloud function
[非免费]。以上代码将向所有订阅 topic_name
的用户发送通知。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。