每天触发多个通知

时间:2020-04-19 19:15:07

标签: java android android-workmanager

我想每天在特定时间(t1,t2,t3,t4,t5)触发5条通知。我必须每天从JSON内部获取这些时间变量,因为它们每天都是不同的值。即使Android应用程序在后台,也必须触发该通知。

MyWorker类

--mount 'type=bind,source="c:/host/folder/path/with/forward/slashes",target="C:/container/folder/path/with/forward/slashes"'

我的LoginActivity

div {
  position: absolute;
  width: 80%;
  height: 40%;
  background-color: red;
}

}

我需要一些帮助来澄清。我应该使用两名工作人员,一个工作人员每天在特定时间从json中获取数据,之后再为每个工作人员使用特定的计时器触发5条通知,或者我该如何解决。非常感谢您的宝贵时间。谢谢!

================================================ ====================

一天后,我也尝试过此方法,但没有用。结合了Service,BroadcastReceiver和AlarmManager。

公共类MyService扩展了服务{

<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" type="text/css" href="wut.css">
</head>

<body>

  <div>

  </div>

</body>

</html>

}

和BroadcastReceiver

公共类ShortTimeEntryReceiver扩展了BroadcastReceiver {

public Result doWork() {
    sendNotification();
    return Result.SUCCESS;
}

public void sendNotification() {
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    //If on Oreo then notification required a notification channel.
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "default")
            .setContentTitle("Hello")
            .setContentText("there")
            .setSmallIcon(R.mipmap.ic_launcher);

    notificationManager.notify(1, notification.build());
}

}

1 个答案:

答案 0 :(得分:0)

WorkManager不保证准确地运行您的工作程序。您需要将AlarmManager与setExact()一起使用。

我个人会考虑在适当的时候使用从服务器发送到应用的High Priority FCM的选项。
对于用户而言,这对电池更加友好,并且由于电池优化,您对现代Android的约束也要少得多。