Search code examples
androidandroid-notificationsjobservice

Can I send Notification using job scheduler?


I'm trying to have Notification very time the Job service run. It's not working. I want to know the right way of providing notification when the Job service is enabled. Please help me in understanding these concepts and how to make it work? Note: Scheduling is working, I'm trying to add Notification, which is not working.

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() {  
    ......
}

}


Solution

  • I think there are some potential problems

    • Forgot to add GuardianJobService in AndroidManifest.xml
    • Never meet the condition to run the job (could you show your schedule code?)
    • If you run on device with Android >= 8.0, you need to create Notification Channel in order to receive notification (refer here)