当我的应用程序在 Google Play 商店更新时,如何保持我的应用程序的通知显示? [英] How can I keep my app's notification displayed when my app is updated on Google Play store?

查看:98
本文介绍了当我的应用程序在 Google Play 商店更新时,如何保持我的应用程序的通知显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我知道...

  • 当我的应用被卸载时,通知被取消.
  • 实际上更新就是卸载并安装".

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

How can I keep displayed when my app version is updated?

推荐答案

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

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>

并实现接收器:

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

这篇关于当我的应用程序在 Google Play 商店更新时,如何保持我的应用程序的通知显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆