整合Google Play服务成就和Android通知

时间:2013-11-20 07:09:44

标签: android notifications google-play-services achievements

我正在使用Google Play服务为Android游戏设置成就。

目标: 在我的onAchievmentUnlocked回调中,我想向用户发送通知,当用户触摸通知时(无论他们身在何处),该通知会打开成就屏幕。

什么有用

  • 通知正确发送,所有图标等都可见。触摸notificatoin虽然没有任何作用。
  • 成就活动确实有效,因为我有一个选项菜单项可以通过以下代码在应用中调用它:activity.startActivityForResult(gameClient.getAchievementsIntent(), ACHIEVEMENTS_ID);

什么不起作用: 触摸通知没有明显效果。

备注

  • MinSDKVersion是14
  • TargetSDKVersion是16

这是我目前的代码:

@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 个答案:

答案 0 :(得分:0)

在此处使用此代码:

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;
            }
        }

    });

此代码将根据您想要的步数增加成就,然后调用onResult以查看增加的成就状态。

以下链接可查看更多可能的状态:https://developer.android.com/reference/com/google/android/gms/games/achievement/Achievements.UpdateAchievementResult.html

相关问题