Skip to content

Instantly share code, notes, and snippets.

@masaibar
Last active January 17, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaibar/70e6d52b26438718ef235eb2f943e1e9 to your computer and use it in GitHub Desktop.
Save masaibar/70e6d52b26438718ef235eb2f943e1e9 to your computer and use it in GitHub Desktop.
FirebaseのRemoteConfigを用いてAndoridアプリのアップデート訴求通知を出す方法を考えてみた ref: http://qiita.com/masaibar/items/f32a788159f210abd262
<?xml version="1.0" encoding="utf-8"?>
<defaultMap>
<entry>
<key>latestVersionCode</key>
<value>0</value>
</entry>
<entry>
<key>showNotificationFlag</key>
<value>false</value>
</entry>
<entry>
<key>notificationTicker</key>
<value>ストアに最新のバージョンがあります。アップデートしてください。</value>
</entry>
<entry>
<key>notificationTitle</key>
<value>最新バージョンがあります</value>
</entry>
<entry>
<key>notificationContent</key>
<value>ストアで最新版にアップデートする</value>
</entry>
</defaultMap>
public class UpdateChecker {
private static final String KEY_LATEST_VERSION_CODE = "latestVersionCode";
private static final String KEY_SHOW_NOTIFICATION_FLAG = "showNotificationFlag";
private static final String KEY_NOTIFICATION_TICKER = "notificationTicker";
private static final String KEY_NOTIFICATION_TITLE = "notificationTitle";
private static final String KEY_NOTIFICATION_CONTENT = "notificationContent";
private int mConfigId;
public UpdateChecker(int configId) {
mConfigId = configId;
}
public interface UpdateCheckerListener {
void shouldNotify(int latestVersionCode, String ticker, String title, String content);
}
/**
* ticker, title, contentのどれかが(空の文字列)の場合は通知を表示しない。
* ※デフォルト値を適用したい場合にはコンソールで(値なし)を選択する。
*/
public void check(
Activity activity, UpdateCheckerListener listener) {
Context context = activity.getApplicationContext();
FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
initRemoteConfig(remoteConfig);
fetch(activity, remoteConfig);
String ticker = getStringValue(remoteConfig, KEY_NOTIFICATION_TICKER);
String title = getStringValue(remoteConfig, KEY_NOTIFICATION_TITLE);
String content = getStringValue(remoteConfig, KEY_NOTIFICATION_CONTENT);
if (TextUtils.isEmpty(ticker) || TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
return;
}
if (!shouldNotify(context, remoteConfig)) {
return;
}
if (listener == null) {
return;
}
listener.shouldNotify(getLatestVersionCode(remoteConfig), ticker, title, content);
}
}
private void initRemoteConfig(FirebaseRemoteConfig remoteConfig) {
FirebaseRemoteConfigSettings.Builder builder = new FirebaseRemoteConfigSettings.Builder();
if (BuildConfig.BUILD_TYPE.equals("debug")) {
builder.setDeveloperModeEnabled(BuildConfig.DEBUG);
}
remoteConfig.setConfigSettings(builder.build());
remoteConfig.setDefaults(mConfigId);
}
private void fetch(Activity activity, final FirebaseRemoteConfig remoteConfig) {
long catchExpiration = 3600;
if (remoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
catchExpiration = 0;
}
remoteConfig.fetch(catchExpiration)
.addOnCompleteListener(activity, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
return;
}
remoteConfig.activateFetched();
}
});
}
/**
* 通知表示フラグがtrueかつ、現在のバージョンコードよりも新しいアプリがあったらtrueを返す
*/
private boolean shouldNotify(
Context context, FirebaseRemoteConfig remoteConfig) {
//通知表示フラグを確認
if (!remoteConfig.getBoolean(KEY_SHOW_NOTIFICATION_FLAG)) {
return false;
}
int currentVersionCode = getVersionCode(context, context.getPackageName());
return currentVersionCode < getLatestVersionCode(remoteConfig);
}
private int getLatestVersionCode(FirebaseRemoteConfig remoteConfig) {
int latestVersionCode = 0;
try {
latestVersionCode = Integer.parseInt(remoteConfig.getString(KEY_LATEST_VERSION_CODE));
} catch (NumberFormatException ignored) {
}
return latestVersionCode;
}
private String getStringValue(FirebaseRemoteConfig remoteConfig, String key) {
return remoteConfig.getString(key);
}
public int getVersionCode(Context context, String packageName) {
PackageInfo info = null;
try {
info = context.getPackageManager().getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return info == null ? -1 : info.versionCode;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new UpdateChecker(R.xml.remote_config_defaults)
.check(this, new UpdateChecker.UpdateCheckerListener() {
@Override
public void shouldNotify(int latestVersionCode, String ticker, String title, String content) {
//todo latestVersionを比較して、既に出してたら出さない的な処理
//todo 通知を送信する
//todo 通知を送信したlatestVersionを保存
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment