2

我在我的應用程序使用行動後端入門,我能使用此代碼不斷地接收數據:當我的應用程序被激活行動後端處理在後臺連續查詢

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); 

這一切工作正常,但我想用此來通知用戶新的數據可用,當應用程序是而不是活動。

當我關閉應用程序(按回,不在家),我強制消息我的設備,我得到以下錯誤:

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 

我如何能實現我想要什麼?

回答

0

那麼你有兩個不同的問題。

讓我們來談談如何通知用戶新的數據可用,當應用程序不活躍時,實際上,您不必實現另一個意圖服務,CloudBackendAndroidClientSample項目中已經有一個已啓動並正在運行,所有您需要做的就是在您收到新消息後立即向用戶顯示通知。這可以很容易地通過添加以下方法給GCMIntentService類完成:

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!"); 
    } 
} 
... 
} 

現在讓我們來談談你得到的錯誤,這主要是因爲您已啓用選項在您的行動後端入門「的客戶ID擔保」服務器配置頁面,而無需在服務器配置頁面和android應用程序常量文件中指定web_client_id。當前的CloudBackendAndroidClientSample沒有正確處理Oauth授權的湖泊所以,每當您再次啓動應用程序並需要訪問服務器時,都會拋出UserRecoverableAuthIOException,但不會向您顯示OAuth同意對話框,而示例應用程序實際上什麼也不做。 所以爲了解決這個問題,您可以:

  1. (首選)確保申報和本google I/O video around 19'
  2. 描述使用相同的Web客戶端ID在你的應用程序,並在服務器配置(傻)更改示例代碼以顯示異常發生時的Oauth同意對話框,以便用戶可以將您的應用程序授予您的服務器,這種情況在用戶體驗視角中很愚蠢,您可以看看谷歌驅動器教程示例代碼github。 在CloudBackendAndroidClientSample異常在CloudBackendAsync釣到圍繞線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); 
        } 
    ... 
    } 
    
1

我還沒有玩過移動後端入門,但是您的問題聽起來像您想要實施Service並在服務中使用雲端後端。因此,您可以在應用不活動時建立通信。這裏是一個good tutorial

0

這可能是在Activity中,所以當你關閉你的應用程序時,應用程序管理器將會銷燬它。所以最好的事情是使用服務。您可以通過OnServiceConnected方法從Service中獲取數據。你也可以從服務發出通知!