Java 没有通知的Android前台服务-如何?
我已经为我的应用程序中的通知创建了前台服务。它工作正常,问题是启动前台服务,我们需要前台通知,通知您的应用程序正在运行 . 我不想要这个通知。如何在不终止服务的情况下删除该通知 这就是我启动前台服务的方式Java 没有通知的Android前台服务-如何?,java,android,Java,Android,我已经为我的应用程序中的通知创建了前台服务。它工作正常,问题是启动前台服务,我们需要前台通知,通知您的应用程序正在运行 . 我不想要这个通知。如何在不终止服务的情况下删除该通知 这就是我启动前台服务的方式 @Override public void onCreate() { super.onCreate(); stopForeground(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@Override
public void onCreate() {
super.onCreate();
stopForeground(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(serviceChannel);
}
Intent stopSelf = new Intent(this, Notification_Service.class);
stopSelf.setAction("ACTION_STOP_SERVICE");
PendingIntent pStopSelf = PendingIntent
.getService(this, 0, stopSelf
, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(
0, "Close", pStopSelf
).build();
Notification notification = notificationBuilder
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Ask Question")
.setContentText("Ask Question is running")
.addAction(action)
.setPriority(Notification.PRIORITY_MIN)
.build();
notificationManager.notify(1, notification);
startForeground(1, notification);
notificationManager.cancel(1);
SharedPreferences settings = getSharedPreferences("Session", MODE_PRIVATE);
id = settings.getString("id", null);
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
handler.postDelayed(new Runnable() {
public void run() {
startMyOwnForeground();
handler.postDelayed(this, delay);
}
}, delay);
}
private void startMyOwnForeground() {
Log.e("", "service running");
get_notification();
}
获取通知的改装电话:
private void get_notification() {
Call<ArrayList> call = apiInterface.getnotification(ApiClient.pin, id);
call.enqueue(new Callback<ArrayList>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(Call<ArrayList> call, Response<ArrayList> response) {
if (response.body() != null) {
for (int i = 0; i < response.body().size(); i = i + 1) {
ArrayList<String> getdata = (ArrayList<String>) response.body().get(i);
String[] data = getdata.toArray(new String[0]);
if (data[5].equals("0")) {
switch (data[4]) {
case "comment":
send_comment_notification(data[1], data[2], data[3], data[0]);
break;
case "question":
send_question_notification(data[3], data[0], data[1]);
break;
case "answer":
send_answer_notification(data[3], data[0], data[1]);
break;
}
}
}
}
}
@Override
public void onFailure(Call<ArrayList> call, Throwable t) {
}
});
}
private void get_通知(){
Call Call=apinterface.getnotification(ApiClient.pin,id);
call.enqueue(新回调(){
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
对于(int i=0;i
您似乎试图使用“拉”方式而不是“推”方式显示通知。要显示由某些后端系统触发的通知,通常应使用推送通知
只有在无法控制后端系统的情况下,才可以考虑从服务器中定期获取更新。但即便如此,创建您自己的后端(另请参见)也可能是有意义的,它会提取更新,然后向您的应用程序发送推送通知,而不是从服务器提取每个应用程序
正如评论中已经建议的那样,您应该使用FCM将通知从服务器推送到应用程序。查看以获得良好的概述。Android系统和Firebase SDK将为您完成繁重的工作,您的应用程序无需在后台或前台运行即可接收通知
如果您仍然希望从服务器中拉入固定的时间间隔,则应该使用API,甚至使用来实现后台同步机制。然后您可以使用创建“本地”通知。以下是Karsten Planz的实现 您应该使用WorkManager API 实现后台同步机制。然后你可以使用 NotificationBuilder创建“本地”通知 通过以下步骤开始主要活动中的任务:
WorkRequest uploadWorkRequest =
new OneTimeWorkRequest.Builder(NotificationWorker.class)
.build();
WorkManager
.getInstance(MainActivity.this)
.enqueue(uploadWorkRequest);
现在创建一个类NotificationWorker
并扩展Worker
。通过返回Result.retry()
,将您的startMyOwnForeground
放在doWork中,这将生成一个递归函数
public class NotificationWorker extends Worker {
Context context;
public NotificationWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
this.context=context;
}
@Override
public Result doWork() {
startMyOwnForeground();
return Result.retry();
}
private void startMyOwnForeground() {
Log.e("", "service running");
get_notification();
}
private void get_notification() {
Call<ArrayList> call = apiInterface.getnotification(ApiClient.pin, id);
call.enqueue(new Callback<ArrayList>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(Call<ArrayList> call, Response<ArrayList> response) {
if (response.body() != null) {
for (int i = 0; i < response.body().size(); i = i + 1) {
ArrayList<String> getdata = (ArrayList<String>) response.body().get(i);
String[] data = getdata.toArray(new String[0]);
if (data[5].equals("0")) {
switch (data[4]) {
case "comment":
send_comment_notification(data[1], data[2], data[3], data[0]);
break;
case "question":
send_question_notification(data[3], data[0], data[1]);
break;
case "answer":
send_answer_notification(data[3], data[0], data[1]);
break;
}
}
}
}
}
@Override
public void onFailure(Call<ArrayList> call, Throwable t) {
}
});
}
}
公共类NotificationWorker扩展了Worker{
语境;
公共通知员(
@非空上下文,
@非空WorkerParameters(参数){
超级(上下文,参数);
this.context=context;
}
@凌驾
公共成果工作(){
startMyOwnForeground();
返回结果。重试();
}
私有void startMyOwnForeground(){
Log.e(“,”服务运行“);
获取通知();
}
私有void get_通知(){
Call Call=apinterface.getnotification(ApiClient.pin,id);
call.enqueue(新回调(){
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
对于(int i=0;i
您不能,因为这是前台服务的目的。用户应该注意到这一点。根据每个前台服务都必须显示一个通知。那么对于通知,我应该使用哪个服务?除了前台,如果应用程序从任务管理器关闭,所有服务都会被终止。我通过改装请求收到通知。因为,我已经忽略了FCM。让我看看工作管理器和同步适配器。你能解释一下拉和推的区别吗?我正在拉数据以获取通知