简体   繁体   中英

Connect to GoogleApiClient from IntentService

I'm new to Android development and I'm trying to setup a geofence that uses a pending intent to notify the user they've entered the geofence and won a badge. I am using Google Play Games Services to set up the badges/achievements. I want to make the notification clickable so it takes you to your achievements page. This is my IntentService:

public class GeofenceService extends IntentService {
private NotificationManager mNotificationManager;
public static final String TAG = "GeofenceService";
private GoogleApiClient mGoogleApiClient;

public GeofenceService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Games.API)
            .addScope(Games.SCOPE_GAMES)
            .build();
    mGoogleApiClient.connect();

    if (event.hasError()) {
        //TODO handle error
    } else {
        int transition = event.getGeofenceTransition();
        List<Geofence> geofences = event.getTriggeringGeofences();
        Geofence geofence = geofences.get(0);
        String requestId = geofence.getRequestId();

        if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {
            Log.d(TAG, "onHandleIntent: Entering geofence - " + requestId);

            if (mGoogleApiClient.isConnected()){
                sendNotification("+ 100");
            }

        } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
            Log.d(TAG, "onHandleIntent: Exiting Geofence - " + requestId);
        }
    }
}


private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return getString(R.string.geofence_transition_entered);
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return getString(R.string.geofence_transition_exited);
        default:
            return getString(R.string.unknown_geofence_transition);
    }
}

private void sendNotification(String details){
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent gamesIntent = Games.Achievements.getAchievementsIntent(mGoogleApiClient);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            gamesIntent, 0);


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("You got a badge")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(details))
                    .setContentText(details)
                    .setSmallIcon(R.drawable.tour);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(1, mBuilder.build());
}

}

This code gives me to following error and is unable to connect to the GoogleApiClient:

E/PopupManager: No content view usable to display popups. Popups will not be displayed in response to this client's calls. Use setViewForPopups() to set your content view.

How can I connect to GoogleApiClient from a pending intent or how can I make the notification clickable so that it takes me to Google Play Games Services Achievements intent?

I figured out the problem. I was not giving the GoogleApiClient instance enough time to connect. So after building the GoogleApiClient and calling its connect() method, I added this line:

ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);

That solved it for me. Hope this helps anyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM