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

How can I keep my app's notification displayed when my app is updated on Google Play store?

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

@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 store.

更新我的应用程序版本时,通知也会被取消

我知道...

我的应用版本更新后如何保持显示?

如果我没理解错的话,你想在更新后显示通知。 所以你可以实现接收器来监听应用程序的更新或设备的重启并再次显示通知。 添加到您的清单:

    <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
    }
}