问题
I am using Google Play services to set up achievements for an android game.
Goal:
In my onAchievmentUnlocked
callback I want to send a notification to the device that opens the achievement screen when the user touches the notification (from wherever they are).
What works:
- The notification gets sent properly and all the icons etc. are visible. Touching the notificatoin does nothing though.
- The achievement activity does work, since I have an Option Menu item to call it up in the app via this code:
activity.startActivityForResult(gameClient.getAchievementsIntent(), ACHIEVEMENTS_ID);
What's not working: Touching the notification has no apparent effect.
Notes:
- MinSDKVersion is 14
- TargetSDKVersion is 16
Here's the code I have at present:
@Override
public void onAchievementUnlocked(final String id) {
final Achievement ac = mAchievementManager.getUnlockedAchievements().get(id);
assert(ac!=null);
Uri uri = ac.getUnlockedImageUri();
final Context ctx = this;
ImageManager.OnImageLoadedListener listener = new ImageManager.OnImageLoadedListener() {
@Override
public void onImageLoaded(Uri uri, Drawable drawable) {
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Notification.Builder builder = new Notification.Builder(ctx)
.setContentTitle(APP_TITLE_STRING)
.setContentText(ac.getName() + " achievement unlocked")
.setLargeIcon(bitmap)
.setSmallIcon(R.drawable.ic_trophy);
Intent intent = mLoginFragment.getGamesClient().getAchievementsIntent();
PendingIntent pIntent = PendingIntent.getActivity(ctx, AchievementManager.REQUEST_ACHIEVEMENTS, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
builder.setContentIntent(pIntent);
Notification note;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) note = builder.build();
else note = builder.getNotification();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
}
};
mImageManager.loadImage(listener,uri,R.drawable.ic_trophy);
}
回答1:
Use this code here:
Games.Achievements.incrementImmediate(GoogleApiClient apiClient, String id, int numSteps)
.setResultCallback(new ResultCallback<Achievements.UpdateAchievementResult>() {
@Override
public void onResult(UpdateAchievementResult result) {
switch (result.getStatus().getStatusCode()) {
case GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCKED:
// the state of the achievement is STATE_UNLOCKED after an increment operation. Continuing to increment an already unlocked achievement will always return this status.
break;
case GamesStatusCodes.STATUS_ACHIEVEMENT_UNKNOWN:
// the achievement failed to update because could not find the achievement to update.
break;
case GamesStatusCodes.STATUS_ACHIEVEMENT_NOT_INCREMENTAL:
// achievement failed to increment since it is not an incremental achievement.
break;
case GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCK_FAILURE:
// the call to unlock achievement failed.
break;
}
}
});
This code will increment an achievement by how many ever steps you want, and then will call onResult to see the status of your achievement that was incremented.
Here is a link to see more possible statuses: https://developer.android.com/reference/com/google/android/gms/games/achievement/Achievements.UpdateAchievementResult.html
来源:https://stackoverflow.com/questions/20089592/integrating-google-play-services-achievements-and-android-notifications