java - 当我的应用程序在 Google Play 商店更新时,如何保持显示应用程序的通知?

标签 java android notifications updates

我的应用程序有设置通知的功能。这是我的代码。

@SuppressWarnings("deprecation")
public static void setNotification(Context _context) {
    NotificationManager notificationManager =
            (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
    int icon_id = R.drawable.icon;

    Notification notification = new Notification(icon_id,
            _context.getString(R.string.app_name), System.currentTimeMillis());
    //
    Intent intent = new Intent(_context, MainActivity.class);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    PendingIntent contextIntent = PendingIntent.getActivity(_context, 0, intent, 0);
    notification.setLatestEventInfo(_context,
            _context.getString(R.string.app_name), 
            _context.getString(R.string.notify_summary), contextIntent);
    notificationManager.notify(R.string.app_name, notification);
}

这段代码工作正常。如果我的应用程序关闭,通知将继续显示。 但即使设置了通知,当用户通过 Google Play 商店更新我的应用版本时,通知也会取消。

我知道...

  • 当我的应用被卸载时,通知就会取消。
  • 事实上,更新就是“卸载并安装”。

当我的应用版本更新时,如何保持显示?

最佳答案

如果我理解正确的话,您希望在更新后显示通知。 因此,您可以实现接收器来监听应用程序更新或设备重新启动并再次显示通知。 添加到您的 list :

    <receiver
        android:name=".UpdatingReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

并实现接收器:

public class UpdatingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
        // check is need to show notification
    }
}

关于java - 当我的应用程序在 Google Play 商店更新时,如何保持显示应用程序的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434409/

相关文章:

java - 从泛型获取 ParameterizedType 的类型

Android - Wifimanager 处理 wifi 连接状态

android - 如何使工具栏透明?

android - 当应用程序位于前台时如何向通知托盘显示通知

iOS 如何按天、周和每小时 Swift 创建计划本地通知

android - 如何在 recyclerview 中对列表项进行分类?

java - 解析数据时出错 org.json.JSONException : Value of type java. lang.String 无法转换为 JSONArray

java - 如何更改 Java 中派生类的成员函数的签名

"for"循环中的 Java 最佳实践。如何定义集合?

java - Android/Gradle/Unity - 在 AAR 插件中包含所有依赖项