通知用户新版Android应用

时间:2016-07-14 19:47:55

标签: android google-play android-notifications android-dialog

如果当前安装的应用不是更新版本(在Play商店中可用),我想显示通知或对话框(当应用打开时)。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以按照here所述使用Firebase通知。您可以按照here所述选择细分(例如应用版本)。

答案 1 :(得分:0)

将此依赖项添加到您的gradle文件..

com.github.rampo.updatechecker:library:2.1.8

在您的活动中尝试此代码..

public static String NEW_VERSION = "1.1.0";
public void checkForAppUpdate () {
        try {
            if (!((Activity) context).isFinishing()) {
                    UpdateChecker.setNotice(Notice.NOTIFICATION);
                    UpdateChecker.setNoticeIcon(R.drawable.your_notification_logo);
                    String s = "Hello User, New version of this application is now available on play store.";
                    if (Comparator.isVersionDownloadableNewer((Activity) context, NEW_VERSION)){

                        SharedPreferences pref = context.getSharedPreferences(UpdateChecker.PREFS_FILENAME, 0);
                        boolean b = pref.getBoolean(UpdateChecker.DONT_SHOW_AGAIN_PREF_KEY + NEW_VERSION, false);
                        if (!b) {
                            displayAlertDialogforPlayStore(context, "Update Available", s);
                        }
                    }
}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

功能displayAlertDialogforPlayStore()是......

public void displayAlertDialogforPlayStore(final Context context, String title,
                                               String message) {
        try {
            final AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setIcon(R.drawable.your_notification_logo);
            if (title != null) {
                alert.setTitle(title);
            }
            alert.setMessage(message);

            alert.setPositiveButton("Update Now", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {



                    final String appPackageName = context.getPackageName();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("market://details?id=" + appPackageName));
                    context.startActivity(intent);
                }
            });

            alert.setNegativeButton("Later", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });

            alert.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

此代码显示通知和alertDailog以进行更新。