0

我是一名測試Google Cloud Messaging,我想將數據發送到智能手錶以便在智能手錶應用程序中打開數據通知。Android:GCM和Google API客戶端

所以,我有一個GcmListenerService處理來自GCM(它正在工作)的消息。

現在,我想知道如何實施Google API客戶端。

下面是我做了,但它不工作:

public class GcmMessageHandler extends GcmListenerService { 

private static final String TAG = "GcmMessageHandler"; 

private GoogleApiClient mGoogleApiClient; 

public static final int MESSAGE_NOTIFICATION_ID = 435345; 

public void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
       @Override 
       public void onConnected(Bundle connectionHint) { 
        Log.d(TAG, "onConnected: " + connectionHint); 
        sendNotif(); 
       } 

       @Override 
       public void onConnectionSuspended(int cause) { 
        Log.d(TAG, "onConnectionSuspended: " + cause); 
       } 
      }) 
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(ConnectionResult result) { 
        Log.d(TAG, "onConnectionFailed: " + result); 
       } 
      }) 
      .addApi(Wearable.API) 
      .build(); 
} 

public void sendNotif() { 
    if (mGoogleApiClient.isConnected()) { 
     PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/new_meeting"); 
     putDataMapRequest.getDataMap().putString("title", "title"); 
     putDataMapRequest.getDataMap().putString("content", "content"); 
     PutDataRequest request = putDataMapRequest.asPutDataRequest(); 
     Wearable.DataApi.putDataItem(mGoogleApiClient, request) 
       .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { 
        @Override 
        public void onResult(DataApi.DataItemResult dataItemResult) { 
         if (!dataItemResult.getStatus().isSuccess()) { 
          Log.e(TAG, "buildWatchOnlyNotification(): Failed to set the data, " 
            + "status: " + dataItemResult.getStatus().getStatusCode()); 
         } else { 
          Log.e(TAG, dataItemResult.getStatus().getStatusMessage()); 
         } 
        } 
       }); 

    } else { 
     Log.e(TAG, "buildWearableOnlyNotification(): no Google API Client connection"); 
    } 
} 

@Override 
public void onMessageReceived(String from, Bundle data) { 

    String message = data.getString("message"); 

    this.buildGoogleApiClient(); 
    this.mGoogleApiClient.connect(); 


} 

} 

我在一個活動嘗試,它正在

public class MainActivity extends AppCompatActivity { 

private static final String TAG = "MainActivity"; 
private static final int REQUEST_SIGNUP = 0; 

private GoogleApiClient mGoogleApiClient; 

@InjectView(R.id.btn__login_login) Button _btn_login_login; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
       @Override 
       public void onConnected(Bundle connectionHint) { 
        Log.d(TAG, "onConnected: " + connectionHint); 
       } 

       @Override 
       public void onConnectionSuspended(int cause) { 
        Log.d(TAG, "onConnectionSuspended: " + cause); 
       } 
      }) 
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(ConnectionResult result) { 
        Log.d(TAG, "onConnectionFailed: " + result); 
       } 
      }) 
      .addApi(Wearable.API) 
      .build(); 

    progressDialog = new ProgressDialog(MainActivity.this, R.style.AppTheme_Dark_Dialog); 

    ButterKnife.inject(this); 

    _btn_login_login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sendNotification(); 
     } 
    }); 

} 

private void sendNotification() { 
    if (mGoogleApiClient.isConnected()) { 
     PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/new_meeting"); 

     dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis()); 
     dataMapRequest.getDataMap().putString("title", "This is the title"); 
     dataMapRequest.getDataMap().putString("content", "This is a notification with some text."); 
     PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest(); 
     Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest); 
    } 
    else { 
     Log.e(TAG, "No connection to wearable available!"); 
    } 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    mGoogleApiClient.disconnect(); 
} 
+0

此監聽器位於移動設備端,而不是智能手錶 – bidou88

回答

0

我改變它在我的監聽器和它的工作:

public void buildGoogleApiClient() { 

    Context context = getBaseContext(); 

    mGoogleApiClient = new GoogleApiClient.Builder(context) 
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
       @Override 
       public void onConnected(Bundle connectionHint) { 
        Log.d(TAG, "onConnected: " + connectionHint); 
        sendNotif(); 
       } 

       @Override 
       public void onConnectionSuspended(int cause) { 
        Log.d(TAG, "onConnectionSuspended: " + cause); 
       } 
      }) 
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(ConnectionResult result) { 
        Log.d(TAG, "onConnectionFailed: " + result); 
       } 
      }) 
      .addApi(Wearable.API) 
      .build(); 
}