我是Android开发的新手,我正在尝试设置一个使用待定意图的地理围栏来通知用户他们已进入地理围栏并获得了徽章。我正在使用Google Play游戏服务来设置徽章/成就。我想让通知可以点击,因此它会将您带到您的成就页面。这是我的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());
}
}
此代码让我出现以下错误,无法连接到GoogleApiClient:
E / PopupManager:没有内容视图可用于显示弹出窗口。弹出窗口会 不会显示以响应此客户的电话。使用 setViewForPopups()来设置你的内容视图。
如何从待处理的意图连接到GoogleApiClient,或者如何使通知可以点击以便我将其转到Google Play游戏服务成就意图?
答案 0 :(得分:0)
我弄明白了这个问题。我没有给GoogleApiClient实例足够的时间来连接。因此,在构建GoogleApiClient并调用其connect()方法之后,我添加了以下行:
ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
这解决了我。希望这对任何人都有帮助!