新文章发布时在 android 应用中显示通知
show notification in android app when new article published
我有一个应用程序可以从我网站上的 json 页面获取文章(例如:测试。com/api/v1/latest)
如果用户没有阅读最新文章,我想通知他们!
我保存了用户阅读的最后一篇文章的 id,然后我想获取发表的最后一篇文章的 id(来自此页面:test.com/api/v1/lastid)!
when app is closed and not running <
然后检查:如果网站中的最后一篇文章 ID 大于设备中保存的 ID,则通过通知通知用户!
我想每 3 或 6 小时做一次!我怎样才能做到这一点 ?最好的方法是什么?
ps1:我应该说我在编码或创建 REST 应用程序方面没有任何问题,我正在寻找解决方案或技巧来完成我的工作!
ps2:我使用 intent 服务和 gson 从网站获取文章,我知道 json 数组!
您可以使用 WorkManager (https://developer.android.com/topic/libraries/architecture/workmanager) (for a regulary scheduled task) or Firebase Cloud Messaging (https://firebase.google.com/docs/cloud-messaging/)(从您的服务器推送)。
对于 WorkManager:
将其添加到您的项目中,参见 https://developer.android.com/topic/libraries/architecture/adding-components:
allprojects {
repositories {
jcenter()
google()
}
}
将其添加到您的依赖项中:
dependencies {
def work_version = "1.0.0-alpha03"
implementation "android.arch.work:work-runtime:$work_version-ktx"
// optional - Firebase JobDispatcher support
// this makes WorkManager work better at old Android Versions -
// it depends on your target group if you need that
implementation "android.arch.work:work-firebase:$work_version"
// optional - Test helpers
androidTestImplementation "android.arch.work:work-testing:$work_version"
}
您需要最新版本的支持库。版本号是目标 SDK 版本(测试的最高版本),而不是最低 SDK。您仍然可以在旧 Android 版本中使用它。
然后创建一个 "Worker" 来完成您的工作并安排它(例如从 MainActivity.onCreate())
class UpdateContentWorker: Worker() {
companion object {
val TAG = "UpdateContentWork"
val enqueueLock = Object()
fun enqueuePeriodicallyIfNotDone() {
// Async.disk is a ThreadPool; Just use anything to run it off the UI Thread
Async.disk.submit {
synchronized(enqueueLock) {
val statuses = WorkManager.getInstance().synchronous().getStatusesByTagSync(TAG)
if (!scheduledOrRunning(statuses)) {
WorkManager.getInstance().synchronous().enqueueSync(
PeriodicWorkRequestBuilder<UpdateContentWorker>(1L, TimeUnit.HOURS)
.addTag(TAG)
.setConstraints(
Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
)
}
}
}
}
private fun scheduledOrRunning(statuses: Collection<WorkStatus>): Boolean {
return statuses.find {
it.state == State.RUNNING || it.state == State.BLOCKED || it.state == State.ENQUEUED
} != null
}
}
override fun doWork(): WorkerResult {
// do what the function title says ...
return WorkerResult.SUCCESS
}
}
我有一个应用程序可以从我网站上的 json 页面获取文章(例如:测试。com/api/v1/latest)
如果用户没有阅读最新文章,我想通知他们!
我保存了用户阅读的最后一篇文章的 id,然后我想获取发表的最后一篇文章的 id(来自此页面:test.com/api/v1/lastid)!
when app is closed and not running <
然后检查:如果网站中的最后一篇文章 ID 大于设备中保存的 ID,则通过通知通知用户!
我想每 3 或 6 小时做一次!我怎样才能做到这一点 ?最好的方法是什么?
ps1:我应该说我在编码或创建 REST 应用程序方面没有任何问题,我正在寻找解决方案或技巧来完成我的工作!
ps2:我使用 intent 服务和 gson 从网站获取文章,我知道 json 数组!
您可以使用 WorkManager (https://developer.android.com/topic/libraries/architecture/workmanager) (for a regulary scheduled task) or Firebase Cloud Messaging (https://firebase.google.com/docs/cloud-messaging/)(从您的服务器推送)。
对于 WorkManager:
将其添加到您的项目中,参见 https://developer.android.com/topic/libraries/architecture/adding-components:
allprojects {
repositories {
jcenter()
google()
}
}
将其添加到您的依赖项中:
dependencies {
def work_version = "1.0.0-alpha03"
implementation "android.arch.work:work-runtime:$work_version-ktx"
// optional - Firebase JobDispatcher support
// this makes WorkManager work better at old Android Versions -
// it depends on your target group if you need that
implementation "android.arch.work:work-firebase:$work_version"
// optional - Test helpers
androidTestImplementation "android.arch.work:work-testing:$work_version"
}
您需要最新版本的支持库。版本号是目标 SDK 版本(测试的最高版本),而不是最低 SDK。您仍然可以在旧 Android 版本中使用它。
然后创建一个 "Worker" 来完成您的工作并安排它(例如从 MainActivity.onCreate())
class UpdateContentWorker: Worker() {
companion object {
val TAG = "UpdateContentWork"
val enqueueLock = Object()
fun enqueuePeriodicallyIfNotDone() {
// Async.disk is a ThreadPool; Just use anything to run it off the UI Thread
Async.disk.submit {
synchronized(enqueueLock) {
val statuses = WorkManager.getInstance().synchronous().getStatusesByTagSync(TAG)
if (!scheduledOrRunning(statuses)) {
WorkManager.getInstance().synchronous().enqueueSync(
PeriodicWorkRequestBuilder<UpdateContentWorker>(1L, TimeUnit.HOURS)
.addTag(TAG)
.setConstraints(
Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
)
}
}
}
}
private fun scheduledOrRunning(statuses: Collection<WorkStatus>): Boolean {
return statuses.find {
it.state == State.RUNNING || it.state == State.BLOCKED || it.state == State.ENQUEUED
} != null
}
}
override fun doWork(): WorkerResult {
// do what the function title says ...
return WorkerResult.SUCCESS
}
}