【发布时间】:2017-01-28 21:17:26
【问题描述】:
我是 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() 来设置你的内容视图。
如何从待处理的 Intent 连接到 GoogleApiClient,或者如何使通知可点击,以便将我带到 Google Play Games Services 成就 Intent?
【问题讨论】: