首页 > 解决方案 > 我可以使用作业调度程序发送通知吗?

问题描述

我正在尝试在作业服务运行时发出通知。它不工作。我想知道启用 Job 服务时提供通知的正确方法。请帮助我理解这些概念以及如何使其发挥作用? 注意:计划正在运行,我正在尝试添加通知,但它不起作用。

public class GuardianJobService  extends JobService{
     public final int REQUEST_CODE_ASK_PERMISSIONS = 1001;
@Override
public boolean onStartJob(JobParameters params) {
    enableTracking();
    addNotification();
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    return true;
}

private void addNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.alert_icon)
                    .setContentTitle("Notifications Example")
                    .setContentText("This is a test notification");
    Intent notificationIntent = new Intent(this, LoginActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}
public void enableTracking() {  
    ......
}

}

标签: androidandroid-notificationsjobservice

解决方案


作业调度基本上是在后台运行长任务。您应该在FirebaseMessagingService中添加通知,然后在添加通知后开始在后台执行任务。


推荐阅读