Commit 83644ee9 by Kim Jinsung

FCM関連ロジック修正

デバイストークン取得改善
プッシュ通知受信処理開演
parent d80e4968
...@@ -8,6 +8,8 @@ import android.content.Intent; ...@@ -8,6 +8,8 @@ import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage; import com.google.firebase.messaging.RemoteMessage;
...@@ -19,6 +21,7 @@ import jp.agentec.abook.abv.bl.dto.MemberInfoDto; ...@@ -19,6 +21,7 @@ import jp.agentec.abook.abv.bl.dto.MemberInfoDto;
import jp.agentec.abook.abv.bl.logic.AbstractLogic; import jp.agentec.abook.abv.bl.logic.AbstractLogic;
import jp.agentec.abook.abv.bl.logic.UserAuthenticateLogic; import jp.agentec.abook.abv.bl.logic.UserAuthenticateLogic;
import jp.agentec.abook.abv.cl.util.AppUtil; import jp.agentec.abook.abv.cl.util.AppUtil;
import jp.agentec.abook.abv.cl.util.PreferenceUtil;
import jp.agentec.abook.abv.launcher.android.R; import jp.agentec.abook.abv.launcher.android.R;
import jp.agentec.abook.abv.ui.common.activity.ShowPushMessageDailogActivity; import jp.agentec.abook.abv.ui.common.activity.ShowPushMessageDailogActivity;
import jp.agentec.abook.abv.ui.common.appinfo.AppDefType; import jp.agentec.abook.abv.ui.common.appinfo.AppDefType;
...@@ -46,6 +49,11 @@ public class ABVFcmListenerService extends FirebaseMessagingService { ...@@ -46,6 +49,11 @@ public class ABVFcmListenerService extends FirebaseMessagingService {
} }
@Override @Override
public void onNewToken(@NonNull String token) {
PreferenceUtil.putUserPref(this, AppDefType.UserPrefKey.NEED_SEND_TOKEN, true);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) { public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> msg = remoteMessage.getData(); Map<String, String> msg = remoteMessage.getData();
...@@ -86,7 +94,7 @@ public class ABVFcmListenerService extends FirebaseMessagingService { ...@@ -86,7 +94,7 @@ public class ABVFcmListenerService extends FirebaseMessagingService {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int uniqueId = (int) System.currentTimeMillis(); int uniqueId = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);
notification = getNotificationBuilder(pendingIntent, message).setChannelId(getApplicationContext().getPackageName()).build(); notification = getNotificationBuilder(pendingIntent, message).setChannelId(getApplicationContext().getPackageName()).build();
......
...@@ -4,26 +4,45 @@ import android.content.Context; ...@@ -4,26 +4,45 @@ import android.content.Context;
//import com.google.firebase.iid.FirebaseInstanceId; //import com.google.firebase.iid.FirebaseInstanceId;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import jp.agentec.abook.abv.bl.common.Callback;
import jp.agentec.abook.abv.bl.common.log.Logger; import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.cl.util.ThreadUtil; import jp.agentec.abook.abv.cl.util.ThreadUtil;
import jp.agentec.abook.abv.launcher.android.R; import jp.agentec.abook.abv.launcher.android.R;
public class FcmManager { public class FcmManager {
private static final String TAG = "FcmManager";
/** /**
* FcmTokenを返す。ただし、PushMessageの利用なしの場合は、noneIdを返す。 * FcmTokenを返す。ただし、PushMessageの利用なしの場合は、noneIdを返す。
* @param context Context * @param context Context
* @return String * @return String
*/ */
public static String getFcmToken(Context context) { public static void getFcmToken(Context context, final Callback resultCallback) {
String fcmToken;
if (context.getResources().getInteger(R.integer.push_message) == 1) { if (context.getResources().getInteger(R.integer.push_message) == 1) {
fcmToken = getFcmTokenWithRetryCount(10); FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Logger.w(TAG, "Fetching FCM registration token failed", task.getException());
resultCallback.callback(null);
return;
}
// Get new FCM registration token
String token = task.getResult();
resultCallback.callback(token);
}
});
} else { } else {
fcmToken = "noneId"; resultCallback.callback("noneId");
} }
return fcmToken;
} }
private static String getFcmTokenWithRetryCount(int retryCount) { private static String getFcmTokenWithRetryCount(int retryCount) {
......
...@@ -31,8 +31,14 @@ import android.widget.LinearLayout; ...@@ -31,8 +31,14 @@ import android.widget.LinearLayout;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.ScrollView; import android.widget.ScrollView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
//import com.google.firebase.iid.FirebaseInstanceId; //import com.google.firebase.iid.FirebaseInstanceId;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import org.xwalk.core.XWalkView; import org.xwalk.core.XWalkView;
...@@ -50,6 +56,7 @@ import jp.agentec.abook.abv.bl.acms.type.DownloadStatusType; ...@@ -50,6 +56,7 @@ import jp.agentec.abook.abv.bl.acms.type.DownloadStatusType;
import jp.agentec.abook.abv.bl.acms.type.LoginMode; import jp.agentec.abook.abv.bl.acms.type.LoginMode;
import jp.agentec.abook.abv.bl.acms.type.UpdateSelect; import jp.agentec.abook.abv.bl.acms.type.UpdateSelect;
import jp.agentec.abook.abv.bl.common.ABVEnvironment; import jp.agentec.abook.abv.bl.common.ABVEnvironment;
import jp.agentec.abook.abv.bl.common.Callback;
import jp.agentec.abook.abv.bl.common.CommonExecutor; import jp.agentec.abook.abv.bl.common.CommonExecutor;
import jp.agentec.abook.abv.bl.common.Constant; import jp.agentec.abook.abv.bl.common.Constant;
import jp.agentec.abook.abv.bl.common.Constant.AlertMessageLevel; import jp.agentec.abook.abv.bl.common.Constant.AlertMessageLevel;
...@@ -81,6 +88,7 @@ import jp.agentec.abook.abv.bl.logic.MemoLogic; ...@@ -81,6 +88,7 @@ import jp.agentec.abook.abv.bl.logic.MemoLogic;
import jp.agentec.abook.abv.bl.logic.UserAuthenticateLogic; import jp.agentec.abook.abv.bl.logic.UserAuthenticateLogic;
import jp.agentec.abook.abv.cl.billing.Purchase; import jp.agentec.abook.abv.cl.billing.Purchase;
import jp.agentec.abook.abv.cl.helper.ABVUncaughtExceptionHandler; import jp.agentec.abook.abv.cl.helper.ABVUncaughtExceptionHandler;
import jp.agentec.abook.abv.cl.push.FcmManager;
import jp.agentec.abook.abv.cl.util.AndroidStringUtil; import jp.agentec.abook.abv.cl.util.AndroidStringUtil;
import jp.agentec.abook.abv.cl.util.BitmapUtil; import jp.agentec.abook.abv.cl.util.BitmapUtil;
import jp.agentec.abook.abv.cl.util.PreferenceUtil; import jp.agentec.abook.abv.cl.util.PreferenceUtil;
...@@ -95,6 +103,7 @@ import jp.agentec.abook.abv.ui.common.constant.ErrorCode; ...@@ -95,6 +103,7 @@ import jp.agentec.abook.abv.ui.common.constant.ErrorCode;
import jp.agentec.abook.abv.ui.common.constant.ErrorMessage; import jp.agentec.abook.abv.ui.common.constant.ErrorMessage;
import jp.agentec.abook.abv.ui.common.constant.NaviConsts; import jp.agentec.abook.abv.ui.common.constant.NaviConsts;
import jp.agentec.abook.abv.ui.common.dialog.ABookAlertDialog; import jp.agentec.abook.abv.ui.common.dialog.ABookAlertDialog;
import jp.agentec.abook.abv.ui.common.util.ABVToastUtil;
import jp.agentec.abook.abv.ui.common.util.AlertDialogUtil; import jp.agentec.abook.abv.ui.common.util.AlertDialogUtil;
import jp.agentec.abook.abv.ui.common.util.DisplayUtil; import jp.agentec.abook.abv.ui.common.util.DisplayUtil;
import jp.agentec.abook.abv.ui.common.util.Initializer; import jp.agentec.abook.abv.ui.common.util.Initializer;
...@@ -160,8 +169,18 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co ...@@ -160,8 +169,18 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co
// 更新されてない // 更新されてない
return; return;
} }
// String fcmToken = FirebaseInstanceId.getInstance().getToken(); final Callback resultCallback = new Callback() {
String fcmToken = "abc"; @Override
public Object callback(Object ret) {
String fcmToken = (String)ret;
needSendDeviceToken(fcmToken);
return null;
}
};
FcmManager.getFcmToken(this, resultCallback);
}
private void needSendDeviceToken(String fcmToken) {
boolean isSendSuccess = false; boolean isSendSuccess = false;
// CMSに送信 // CMSに送信
if (getRInteger(R.integer.push_message) == 1 && fcmToken != null) { if (getRInteger(R.integer.push_message) == 1 && fcmToken != null) {
...@@ -173,7 +192,7 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co ...@@ -173,7 +192,7 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co
} else { } else {
AbstractLogic.getLogic(UserAuthenticateLogic.class).updateDeviceTokenByMacAdress(fcmToken); AbstractLogic.getLogic(UserAuthenticateLogic.class).updateDeviceTokenByMacAdress(fcmToken);
} }
isSendSuccess = true; isSendSuccess = true;
} catch (NetworkDisconnectedException e) { } catch (NetworkDisconnectedException e) {
Logger.w(TAG, "[sendRegistrationIdToCMS] NetworkDisconnectedException"); Logger.w(TAG, "[sendRegistrationIdToCMS] NetworkDisconnectedException");
} catch (Exception e) { } catch (Exception e) {
...@@ -181,7 +200,7 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co ...@@ -181,7 +200,7 @@ public abstract class ABVAuthenticatedActivity extends ABVActivity implements Co
} }
} }
// CMSへ更新した結果をセットする // CMSへ更新した結果をセットする
PreferenceUtil.putUserPref(this, UserPrefKey.NEED_SEND_TOKEN, !isSendSuccess); PreferenceUtil.putUserPref(this, UserPrefKey.NEED_SEND_TOKEN, !isSendSuccess);
} }
......
...@@ -3,12 +3,15 @@ package jp.agentec.abook.abv.ui.common.activity; ...@@ -3,12 +3,15 @@ package jp.agentec.abook.abv.ui.common.activity;
import android.widget.Toast; import android.widget.Toast;
import jp.agentec.abook.abv.bl.common.ABVEnvironment; import jp.agentec.abook.abv.bl.common.ABVEnvironment;
import jp.agentec.abook.abv.bl.common.Callback;
import jp.agentec.abook.abv.bl.common.exception.ABVException; import jp.agentec.abook.abv.bl.common.exception.ABVException;
import jp.agentec.abook.abv.bl.common.log.LogLevel; import jp.agentec.abook.abv.bl.common.log.LogLevel;
import jp.agentec.abook.abv.bl.common.log.Logger; import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.cl.environment.DeviceInfo; import jp.agentec.abook.abv.cl.environment.DeviceInfo;
import jp.agentec.abook.abv.cl.push.FcmManager; import jp.agentec.abook.abv.cl.push.FcmManager;
import jp.agentec.abook.abv.cl.util.PreferenceUtil;
import jp.agentec.abook.abv.launcher.android.R; import jp.agentec.abook.abv.launcher.android.R;
import jp.agentec.abook.abv.ui.common.appinfo.AppDefType;
import jp.agentec.abook.abv.ui.common.util.ABVToastUtil; import jp.agentec.abook.abv.ui.common.util.ABVToastUtil;
/** /**
...@@ -24,23 +27,32 @@ public abstract class ABVLoginActivity extends ABVNoAuthenticatedActivity { ...@@ -24,23 +27,32 @@ public abstract class ABVLoginActivity extends ABVNoAuthenticatedActivity {
* 必要なければユーザ情報を保存する。 * 必要なければユーザ情報を保存する。
*/ */
protected void fcmRegister() { protected void fcmRegister() {
String fcmToken = FcmManager.getFcmToken(this); final Callback resultCallback = new Callback() {
try { @Override
if (fcmToken == null) { public Object callback(Object ret) {
Logger.w(TAG, "[fcmRegister]: FCM Token is null."); String fcmToken = (String)ret;
runOnUiThread(new Runnable() { try {
@Override if (fcmToken == null) {
public void run() { Logger.w(TAG, "[fcmRegister]: FCM Token is null.");
ABVToastUtil.showMakeText(ABVLoginActivity.this, R.string.fcm_not_supported, Toast.LENGTH_SHORT); runOnUiThread(new Runnable() {
@Override
public void run() {
ABVToastUtil.showMakeText(ABVLoginActivity.this, R.string.fcm_not_supported, Toast.LENGTH_SHORT);
}
});
// FCMトークンを取得できない場合、noneIdでセットしてログインする
fcmToken = "noneId";
} }
}); serverLoginAndCheckChangeUser(fcmToken);
// FCMトークンを取得できない場合、noneIdでセットしてログインする } finally {
fcmToken = "noneId"; closeProgressPopup();
}
return null;
} }
serverLoginAndCheckChangeUser(fcmToken); };
} finally {
closeProgressPopup(); FcmManager.getFcmToken(this, resultCallback);
}
} }
/** /**
......
...@@ -105,13 +105,20 @@ public abstract class ABVNoAuthenticatedActivity extends ABVActivity { ...@@ -105,13 +105,20 @@ public abstract class ABVNoAuthenticatedActivity extends ABVActivity {
CommonExecutor.execute(new Runnable() { CommonExecutor.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
String fcmToken = FcmManager.getFcmToken(mContext); final Callback resultCallback = new Callback() {
if (fcmToken == null) { @Override
ABVToastUtil.showMakeText(mContext, R.string.fcm_not_supported, Toast.LENGTH_SHORT); public Object callback(Object ret) {
// FCMトークンを取得できない場合、noneIdでセットしてログインする String fcmToken = (String)ret;
fcmToken = "noneId"; if (fcmToken == null) {
} ABVToastUtil.showMakeText(mContext, R.string.fcm_not_supported, Toast.LENGTH_SHORT);
noAuthenticatedShowMain(isGuestLogin, fcmToken, urlPath); // FCMトークンを取得できない場合、noneIdでセットしてログインする
fcmToken = "noneId";
}
noAuthenticatedShowMain(isGuestLogin, fcmToken, urlPath);
return null;
}
};
FcmManager.getFcmToken(mContext, resultCallback);
} }
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment