简体   繁体   English

应用在Android中一天后台运行后,如何发出本地通知

[英]How can I make an local notification after app go to background a day in Android

I want develop a remind return feature for my Android app. 我想为我的Android应用开发提醒返回功能。 I want after a day from when user not use app, I will show a local notification, remind user that "We have more new content, touch here to read new content". 我希望从用户不使用应用程序的一天起,我会显示本地通知,提醒用户“我们还有更多新内容,请触摸此处以阅读新内容”。 But I don't know how to code. 但是我不知道如何编码。

  • I don't know how to detect app is begin in to background (onPause can fire on app enter background, but also when app change activity). 我不知道如何检测应用程序是从后台开始的(onPause可以在应用程序进入后台时触发,也可以在应用程序更改活动时触发)。

  • How can I fire notification when app in background. 当应用在后台运行时,如何触发通知。 I have this function to show notifcation, but it show after I call it. 我有此功能来显示通知,但在我调用它后会显示。 How can I delay it a day. 我该如何延迟一天。

showNotification() 显示通知()

NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.down_icon, getString(R.string.press_back_again_if_you_want_to_exit_app), System.currentTimeMillis());

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

notification.setLatestEventInfo(this, this.getString(R.string.press_back_again_if_you_want_to_exit_app),
        this.getString(R.string.press_back_again_if_you_want_to_exit_app), pendingIntent);
notificationManager.notify(1, notification);

I develop feature very easy in iOS, but in Android, I don't know how to do. 我在iOS中开发功能非常容易,但是在Android中,我不知道该怎么做。

to achieve your requirement follow the below steps. 要满足您的要求,请执行以下步骤。

step1- When your app starts start a background service. 步骤1-当您的应用启动时,启动后台服务。 In the service check the top most activity every time interval 在服务中,每个时间间隔检查最活跃的活动

step2-In service when the top most activity not belongs to your app then schedule the alarm to fire after 24 hrs and destroy the service. 步骤2:在最活跃的活动不属于您的应用程序时进入服务状态,然后安排警报在24小时后触发并破坏服务。

To send a notification from the background, you will use a Service or a BroadcastReceiver, not an Activity. 要从后台发送通知,您将使用服务或BroadcastReceiver,而不是Activity。 I think the BroadcastReceiver will best suit your necessity: http://developer.android.com/reference/android/content/BroadcastReceiver.html 我认为BroadcastReceiver最适合您的需要: http : //developer.android.com/reference/android/content/BroadcastReceiver.html

To schedule the execution, use the AlarmManager. 要安排执行时间,请使用AlarmManager。 You can set the alarm during the other Activity lifecycle methods: http://developer.android.com/training/basics/activity-lifecycle/index.html 您可以在其他“活动”生命周期方法中设置警报: http : //developer.android.com/training/basics/activity-lifecycle/index.html

For that you need to write service which check "activity state is in OnPause() state?" 为此,您需要编写检查“活动状态是否处于OnPause()状态?”的服务。 in every time interval. 在每个时间间隔。 and after that you can fire notification whatever you want. 之后,您可以触发任何所需的通知。 thank you. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 android 应用程序在收到推送通知后在后台启动,以便它可以从服务器获取数据? - How can I make an android app start in the background on receipt of a push notification so it can fetch data from the server? 我可以通过android phonegap应用中的服务器发出带有可变消息的每日本地通知吗? - Can I make a daily local notification with a variable message from the server in an android phonegap app? 如何使我的android应用在后台运行? - How can I make my android app run on the background? 如何在Android中进行本地通知? - How to make Local Notification in android? 如何在Android中进行本地通知? - How to make Local Notification in android? 如何在 flutter 后台收到本地通知时调用 function - How can I call a function on local notification received in background in flutter Flutter:Android:如何制作带有一些操作按钮的不可移动的本地通知工具栏,如屏幕截图所示 - Flutter: Android: How can i make unremovable local notification toolbar with some action button on it as shown in screenshot 如何在 android 中发出振动通知? - How can I make a vibrating notification in android? 在后台收到通知时,如何让我的android应用执行代码? - How can I get my android app to execute code when a notification is received when it's in background? Xamarin.Android-使用本地通知关闭应用后,使其在后台运行 - Xamarin.Android - Keep the app working in background after closing it with a local notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM