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

@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商店更新我的应用版本时,通知也会被取消。

我知道...


卸载我的应用程序后,通知将被取消。
实际上,更新是“卸载并安装”。


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

最佳答案

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

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

10-09 15:57
查看更多