移动后端处理后台的连续查询 [英] Mobile Backend handle continuous queries in background

查看:161
本文介绍了移动后端处理后台的连续查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用行动后端入门我的应用程序,我能连续接收使用此code数据:

I'm using Mobile Backend Starter in my app, and I'm able to continuously receive data using this code:

CloudCallbackHandler<List<CloudEntity>> handler = new CloudCallbackHandler<List<CloudEntity>>() {
        @Override
        public void onComplete(List<CloudEntity> results) {
            Logger.log(MainActivity.this, "onComplete");
        }

        @Override
        public void onError(IOException e) {
            Logger.log(MainActivity.this, e);
        }
    };

    CloudQuery cq = new CloudQuery("Test");
    cq.setLimit(50);
    cq.setSort(CloudEntity.PROP_UPDATED_AT, Order.DESC);
    cq.setScope(Scope.FUTURE_AND_PAST);
    getCloudBackend().list(cq, handler);

这一切工作正常,当我的应用程序是积极的,但我想用它来通知用户新的数据是可用的,当应用程序是的的活跃。

This all works fine when my app is active, but I want to use this to notify the user new data is available, when the app is not active.

当我关闭应用程序(由pressing回来了,不在家),我强制消息给我的设备,我得到以下错误:

When I close the app (by pressing back, not home), and I force a message to my device, I get the following error:

07-04 18:30:23.084: I/CloudBackend(31368): error: 
07-04 18:30:23.084: I/CloudBackend(31368): com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:222)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.myapp.cloudbackend.CloudBackend.list(CloudBackend.java:340)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.myapp.cloudbackend.CloudBackendAsync.access$8(CloudBackendAsync.java:1)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.myapp.cloudbackend.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:283)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.myapp.cloudbackend.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:1)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.myapp.cloudbackend.CloudBackendAsync$BackendCaller.run(CloudBackendAsync.java:429)
07-04 18:30:23.084: I/CloudBackend(31368): Caused by: com.google.android.gms.auth.UserRecoverableAuthException: AppDownloadRequired
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
07-04 18:30:23.084: I/CloudBackend(31368):  at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
07-04 18:30:23.084: I/CloudBackend(31368):  ... 9 more

如何才能实现我想要什么?

How can I achieve what I want?

推荐答案

那么你有两个不同的问题。

Well you have two distinct problems.

让我们来谈谈如何通知用户新的数据是可用的,当应用程序不活跃,其实,你也不必实现另一个目的的服务,已经有一上来,并在CloudBackendAndroidClientSample项目运行时,所有你需要做的是尽快显示给用户的通知,你得到新的消息。这可以很容易地通过将以下方法将GCMIntentService类来完成:

Let's talk about how to notify the user new data is available, when the app is not active, actually, you don't have to implement another intent service, there is already one up and running in the CloudBackendAndroidClientSample project, all you have to do is show up a notification to the user as soon as you get new message. This can easily be done by adding the following method to the GCMIntentService class:

public class GCMIntentService extends GCMBaseIntentService {
...
//add this method
public void showNotification(Context context, int id, String title, String message){
    Intent intent = new Intent(context, GuestbookActivity.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context.getApplicationContext())
            .setTicker(message)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .setOngoing(false)
            .setOnlyAlertOnce(true)
            .setLights(0xFFFF0000, 500, 500); //setLights (int argb, int onMs, int offMs)
    Notification notification = mBuilder.build();

    // show it in the notification list
    notification.setLatestEventInfo(context, title, message, pendingIntent);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, notification);
    Log.d(TAG, "notification should be shown");
}
...
//And simply call that function in your onMessage callback
@Override
public void onMessage(Context context, Intent intent) {
    // decode subId in the message
    String subId = intent.getStringExtra(GCM_KEY_SUBID);
    Log.i(Consts.TAG, "onMessage: subId: " + subId);
    String[] tokens = subId.split(":");
    String typeId = tokens[1];

    // dispatch message
    if (GCM_TYPEID_QUERY.equals(typeId)) {
        CloudBackendAsync.handleQueryMessage(tokens[2]);
        Log.e(TAG, "update notif");
        //call the fonction to show up your notification here
        showNotification(context, 1, getString(R.string.app_name), "new messages!");
    }
}
...
}

现在让我们来谈谈你得到的错误,这主要是因为你已经启用了您的移动后端入门服务器配置页选项,而不在两个指定web_client_id的客户ID担保:服务器配置页面和Android应用consts文件。目前CloudBackendAndroidClientSample不能正确处理OAuth授权的湖所以每次你再次启动应用程序,它需要访问服务器,UserRecoverableAuthIOException被抛出,但不是显示你的OAuth同意对话框中的示例应用程序实际上什么都不做。
因此,为了解决这个问题,您可以:

Now Let's talk about the error you got, this is mainly because you have enabled "Secured by Client IDs" option in your Mobile Backend Starter server config page without specifying a web_client_id in both : server config page and android app consts file. The current CloudBackendAndroidClientSample does not properly handle the lake of Oauth authorization So everytime you start the app again and it need to access the server, a UserRecoverableAuthIOException is thrown but instead of showing you the OAuth consent dialog the sample app does actually nothing. So in order to solve this problem, you can either:


  1. (preferred)确保申报和本所描述的谷歌I / O约为19'
  2. (傻)更改采样code,显示每当例外发生在OAuth同意对话框,让用户可以授予访问你的应用程序到您的服务器,这是从一个用户体验的角度傻情况下,你可以看看一个谷歌的驾驶教程样本code。关于<一个href=\"https://github.com/alberovalley/Google-Drive-Tutorial/blob/master/src/com/alberovalley/gdrivetutorial/MainActivity.java\"相对=nofollow> github上。
    在CloudBackendAndroidClientSample异常被逮住在CloudBackendAsync周围管线430

  1. (Preferred) Make sure you declared and use the same web client id in your app and in your server config as described in this google I/O video around 19'
  2. (Silly) Change the sample code to show the Oauth consent dialog whenever the exception occur, so that the user can grant access your app to your server, which is silly from a User Experience perspective in that case, you can take a look a the google drive tutorial sample code on github. In CloudBackendAndroidClientSample the exception is catched in CloudBackendAsync around line 430

private abstract class BackendCaller<PARAM, RESULT> extends Thread {  
...

@Override
public void run() {
...

   try{
      r = callBackend(param);
   } 
   catch (IOException e) {
      Log.i(Consts.TAG, "error: ", e);
      //The UserRecoverableAuthIOException is only logged here, a OAuth Consent Dialog should be opened to grant access to the server.
      ie = e;
   }

   //So you should change this catch by something like the following. 
   //Of course you will need an activity context to be able to call startActivityForResult, that is why I said you will need a little bit of refactoring.
   catch (UserRecoverableAuthIOException e) {
      startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
   }
...
}


这篇关于移动后端处理后台的连续查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆