问题描述
如何在不强制用户打开应用程序的情况下设置本地通知。
我需要我的应用为日出和日落设置本地通知,但我不想让人们打开应用。
我知道我最多可以通过 scheduleLocalNotification 收到64条通知,但是我需要将其设置为一年,因此我应该能够在后台运行应用程序并为未来的日出和日落设置警报
How can I set local notifications with out forcing user to open app.I need my app set a local notification for sunrise and sunset, but I don't want to ask people open app.I know I can have up to 64 notifications via scheduleLocalNotification, but I need to set it for a year so I should be able to run app in background and set alarms for future sunrises and sunsets in background.
推荐答案
具有新功能为您的用例提供了一个简单的解决方案(避免您必须显式管理位置请求或计算日出时间)。实现您要执行的操作的方法是创建并注册相对于日出/日落指定的 TimeFence
。
Android Awareness API has recently announced new features that provide a simple solution for your use-case (that avoids you having to explicitly manage location request or computing sunrise times). The way to achieve what you're trying to do is to create and register a TimeFence
specified relative to sunrise/sunset.
例如:
// Create TimeFence
AwarenessFence sunriseFence =
TimeFence.aroundTimeInstant(TimeFence.TIME_INSTANT_SUNRISE,
0, 5 * ONE_MINUTE_MILLIS);
// Register fence with Awareness.
Awareness.FenceApi.updateFences(
mGoogleApiClient,
new FenceUpdateRequest.Builder()
.addFence("fenceKey", sunriseFence, myPendingIntent)
.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Fence was successfully registered.");
} else {
Log.e(TAG, "Fence could not be registered: " + status);
}
}
});
当篱笆评估为 TRUE $ c $时,您将获得回调c>日出时,并且在日出后5分钟返回到
FALSE
的情况。
You will get callbacks when the fence evaluates to TRUE
at sunrise, and when it evaluates back to FALSE
at 5-min after sunrise.
请检查 Fence API代码段文档
这篇关于应用关闭时的设置警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!