6. private boolean isForeground() {
// Gets a list of running processes.
ActivityManager am = (ActivityManager)
pinpointContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
// On some versions of android the first item in the list is what runs in the foreground,
// but this is not true on all versions. Check the process importance to see if the app
// is in the foreground.
final String packageName = pinpointContext.getApplicationContext().getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : tasks) {
final String processName = appProcess.processName;
if (ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND == appProcess.importance
&& packageName.equals(processName)) {
return true;
}
}
return false;
}
対策:ライブラリ内メソッドの踏襲
AWS SDK内でAndroid4以下のフォアグラウンドの判定が間違ってる (・∀・)カエレ!!
7. 対策:ライブラリ内メソッドの踏襲
if (NotificationClient.CampaignPushResult.APP_IN_FOREGROUND.equals(pushResult)) {
// If the app was in the foreground, you may want to add logic here to send
// a broadcast back to your main activity to show something to the user.
appContext = getApplicationContext();
// AWS SDKの処理ではAndroid Forgroundの判定が足りないので正しく判定(Android 4)
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP){
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
if (am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(getApplicationContext().getPackageName())) {
// ここにフォアグラウンドの場合の処理を実装
showPopup(data);
}
else{
// バックグラウンドの処理はAWS SDK内部処理を踏襲したメソッドを実装・・・(通知バナー表示)
handleCampaignPush(data,this.getClass());
}
}
else{
// Android 5以上のフォアグラウンド処理
showPopup(data);
}
}
// AWS SDKライブラリ内部のメソッドをアレンジ/(^o^)\
private void handleCampaignPush(final Bundle data, final Class<?> targetClass) {
・・・
}