android,如何使用 PendingIntent 在 Playstore 表单 FCM 通知中打开应用
android, how to open app in playstore form FCM notification with PendingIntent
当以编程方式打开 playstore 时,它可以使用 try/catch
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appName));
intent.setPackage("com.android.vending");
startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+appName));
intent.setPackage("com.android.vending");
startActivity(intent);
}
并且当收到FCM消息时,需要将intent放在PendingIntent中,android点击通知时会做startactivity()
Intent intent;
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (Throwable ex) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
intent.setPackage("com.android.vending");
PendingIntent pendingIntent = PendingIntent.getActivity(context, nextNotificationId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
... ... ...
它在安装了 Playstore 应用程序的设备上工作,但不执行任何操作(当点击带有此 PendingIntent 的通知时),并且 "market://details?id=" + appPackageName
不会导致异常。
尝试发现在没有安装playtore app的设备上,下面一行会在浏览器中打开playstore:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
没有将 com.android.vending
设置为意图包的行:
intent.setPackage("com.android.vending");
所以问题是如何检查设备上是否安装了playstore应用程序?
如果它可以检测到它可以有条件地选择工作代码。
在同一台设备上试过,returns com.android.vending
正确
private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";
void someMethod() {
PackageManager packageManager = getApplication().getPackageManager();
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo packageInfo : packages) {
if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) ||
packageInfo.packageName.equals(GooglePlayStorePackageNameNew)) {
googlePlayStoreInstalled = true;
break;
}
}
}
并且 isGooglePlayServicesAvailable(Context)
也是如此。
ActivityNotFoundException
由 startActivity
抛出,以防没有 Activity
找到 运行 给定 Intent。
如果您不直接调用 startActivity()
意图,您可以使用 Intent#resolveActivity 验证是否存在接收意图的应用程序
Intent intent;
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appPackageName));
if (intent.resolveActivity(context.getPackageManager()) == null) {
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
PendingIntent pendingIntent = PendingIntent.getActivity(
context, nextNotificationId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
当以编程方式打开 playstore 时,它可以使用 try/catch
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appName));
intent.setPackage("com.android.vending");
startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+appName));
intent.setPackage("com.android.vending");
startActivity(intent);
}
并且当收到FCM消息时,需要将intent放在PendingIntent中,android点击通知时会做startactivity()
Intent intent;
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (Throwable ex) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
intent.setPackage("com.android.vending");
PendingIntent pendingIntent = PendingIntent.getActivity(context, nextNotificationId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
... ... ...
它在安装了 Playstore 应用程序的设备上工作,但不执行任何操作(当点击带有此 PendingIntent 的通知时),并且 "market://details?id=" + appPackageName
不会导致异常。
尝试发现在没有安装playtore app的设备上,下面一行会在浏览器中打开playstore:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
没有将 com.android.vending
设置为意图包的行:
intent.setPackage("com.android.vending");
所以问题是如何检查设备上是否安装了playstore应用程序? 如果它可以检测到它可以有条件地选择工作代码。
在同一台设备上试过,returns com.android.vending
正确private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";
void someMethod() {
PackageManager packageManager = getApplication().getPackageManager();
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo packageInfo : packages) {
if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) ||
packageInfo.packageName.equals(GooglePlayStorePackageNameNew)) {
googlePlayStoreInstalled = true;
break;
}
}
}
并且 isGooglePlayServicesAvailable(Context)
也是如此。
ActivityNotFoundException
由 startActivity
抛出,以防没有 Activity
找到 运行 给定 Intent。
如果您不直接调用 startActivity()
意图,您可以使用 Intent#resolveActivity 验证是否存在接收意图的应用程序
Intent intent;
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appPackageName));
if (intent.resolveActivity(context.getPackageManager()) == null) {
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
PendingIntent pendingIntent = PendingIntent.getActivity(
context, nextNotificationId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);