2

通知をスケジュールするには:

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」が表示されます)。ただし、通知は表示されません (エラーがスローされることはありません)。

通知が発生しない理由を知っていますか? 私は何か間違ったことをしていますか?

4

1 に答える 1