简体   繁体   English

即使应用未运行,用户也如何接收通知

[英]How is it possible for the user to receive notifications even when the app is not running

So I am trying to make an app so that when a row in a SQL database changes, the user receives a notification even though the app is not currently running. 因此,我试图制作一个应用程序,以便当SQL数据库中的行发生更改时,即使该应用程序当前未运行,用户也会收到通知。 I'm pretty sure this is possible because Facebook can send mobile notifications when the app isn't running, but I'm not sure how to go about this. 我很确定这是可能的,因为Facebook可以在应用未运行时发送移动通知,但是我不确定该如何处理。

You need to use Service which runs in background. 您需要使用在后台运行的服务 From within a service you can start a notification. 在服务中,您可以启动通知。 Its not clear that which 'notification' are you talking about? 目前尚不清楚您在谈论哪个“通知”? If you are talking about notification as in Google Cloud Messaging notification , then you need to go a different way. 如果您要像在Google Cloud Messaging notification中那样谈论通知 ,那么您需要采取其他方法。 But, then also you would be using GCMIntentService which extends Service class. 但是,那么您还将使用扩展Service类的GCMIntentService

You want to send a notification when when a row in a SQL database changes . when a row in a SQL database changeswhen a row in a SQL database changes您要发送通知。 Is that row changing on the server side? 该行在服务器端更改了吗? I am assuming yes because first you were talking about Android database, then you would have mentioned SQLite instead of SQL. 我假设是的,因为首先您是在谈论Android数据库,然后您会提到SQLite而不是SQL。 Secondly you gave example of Facebook. 其次,您举了Facebook的例子。 So, if the answer to my question is yes, then using GCM push services you can send a Push Message to the user. 因此,如果对我的问题的回答是“是”,则可以使用GCM推送服务向用户发送“推送消息”。 Then when user receives the message, you can show a notification with the proper data. 然后,当用户收到消息时,您可以显示带有适当数据的通知。 In the onReceive method of GCMIntentService , you will receive the content in an Intent. GCMIntentServiceonReceive方法中,您将以Intent接收内容。 There you can extract the message and create a notification. 您可以在那里提取消息并创建通知。 See here for more. 看到这里更多。

You will need to register a content observer to get notified of the changes. 您将需要注册内容观察者以获取有关更改的通知。

To use the ContentObserver you have to take two steps: 要使用ContentObserver,您必须执行两个步骤:

  • Implement a subclass of ContentObserver 实现ContentObserver的子类
  • Register your content observer to listen for changes 注册您的内容查看器以收听更改
  • Notify change from the content provider 通知内容提供商的更改

-- -

class DbObserver extends ContentObserver {      
   public DbObserver(Handler handler) {
      super(handler);           
   }

   @Override
   public void onChange(boolean selfChange) {
      this.onChange(selfChange, null);
      // start the notification
   }        
}

Register your content observer to listen for the changes: 注册您的内容查看器以侦听更改:

getContentResolver().
      registerContentObserver(
            SOME_URI, 
            true, 
            yourObserver);

Now can call notifyChange after updating your db. 现在可以在更新数据库后调用notifyChange

notifyChange(Uri uri, ContentObserver observer)

A quick example of using content observer is https://gist.github.com/JBirdVegas/3874450 一个使用内容观察器的快速示例是https://gist.github.com/JBirdVegas/3874450

You can use Service in android 您可以在android中使用Service

A service by default runs in the same process in the main thread as the application. 默认情况下,服务在与应用程序相同的主线程中运行。

Therefore you need to use asynchronous processing in the service to perform resource intensive tasks in the background. 因此,您需要在服务中使用异步处理以在后台执行资源密集型任务。 A common used pattern for a service implementation is to create and run a new Thread in the service to perform the processing in the background and then to terminate the service once it has finished the processing. 服务实现的常用模式是在服务中创建并运行新线程以在后台执行处理,然后在服务完成处理后终止服务。

Each Service has a specific job, and it will run continuously even if you switch between different Activities, or to a different application altogether. 每个服务都有一个特定的作业,即使您在不同的活动之间切换或完全切换到另一个应用程序,它也会连续运行。

To start a service use the following: 要启动服务,请使用以下命令:

Intent i= new Intent(context, ServiceClass.class);
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i); 

A simple example for a service class: 服务类的一个简单示例:

public class ServiceClass extends Service {
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_NOT_STICKY;
      }
      @Override
      public IBinder onBind(Intent intent) {
        return null;
      }
    } 

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

相关问题 即使应用被杀死,如何继续接收地理围栏通知? - How to receive continuous geofence notifications even when app is killed? Firebase云消息传递服务 - 即使应用程序被终止也会收到通知 - Firebase Cloud Messaging Service - Receive Notifications even when app is killed 即使应用程序被杀死,whatsapp、messenger 和其他非常著名的消息传递应用程序如何接收推送通知? - How does whatsapp, messenger and other very famous messaging apps receive push notifications even when app is killed? 应用程序被杀死时是否可以接收 FCM 推送通知? - Is it possible to receive FCM push notifications when app is killed? 如何在我的应用未运行时接收通知? - How to receive Notifications while my app isn't running? Android/Java:即使未“主动”使用应用程序,如何向用户发送通知? - Android/Java: how to send notifications to user, even when app is not "actively" being used? 即使用户切换应用程序,CountDownTimer也会运行 - CountDownTimer running even when user switches app 即使应用未运行,如何显示通知 - How do I show notifications even if the app is not running 应用程序运行时如何清除通知? - How to clear notifications when the app is running? 应用被杀死时接收PubNub推送通知 - Receive PubNub Push Notifications when app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM