gpt4 book ai didi

java - FirebaseJobDispatcher - 显示来自 JobService 的通知

转载 作者:行者123 更新时间:2023-11-29 02:30:56 29 4
gpt4 key购买 nike

安排通知:

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));

Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class) // the JobService that will be called
.setTag("my-unique-tag") // uniquely identifies the job
.setTrigger(Trigger.executionWindow(0, 5))
.build();

dispatcher.mustSchedule(myJob);

工作服务:

public class MyJobService extends JobService
{
@Override
public boolean onStartJob(JobParameters job)
{
// Do some work here

sendNotification("TEST");

return true; // Answers the question: "Is there still work going on?"
}

@Override
public boolean onStopJob(JobParameters job)
{
return false; // Answers the question: "Should this job be retried?"
}

private void sendNotification(String messageBody)
{
Log.d("TEST","Notification: " + messageBody);

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Job executed")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

作业正在按预期执行(我可以在 logcat 中看到“TEST”)。但是,不会出现通知(没有抛出任何错误)。

您知道为什么不触发通知吗?我做错了什么吗?

最佳答案

在onStartJob方法中设置返回false

public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters job)
{
// Do some work here

sendNotification("TEST");

return false; // Answers the question: "Is there still work going on?"
}...}

关于java - FirebaseJobDispatcher - 显示来自 JobService 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49645837/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com