问题
My app has a function of set-notification. Here is my code.
@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);
}
This code works fine. If my app is closed, notification keeps displayed. But even if notification was set, the notification is cancelled when user will update my app version by Google Play store.
I know that...
- The notification is cancelled when my app is uninstalled.
- In fact an update is "uninstall and install".
How can I keep displayed when my app version is updated?
回答1:
If I understood right you want displaying a notification after updating. So you can implement receiver that listens updating of app or rebooting of device and shows notification again. add to you manifest:
<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>
and implement 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
}
}
来源:https://stackoverflow.com/questions/35434409/how-can-i-keep-my-apps-notification-displayed-when-my-app-is-updated-on-google