当应用进入后台时,通知会显示上一次通知中的内容

时间:2015-12-24 10:36:00

标签: android android-activity push-notification inappbrowser

我在活动ViewInWeb中实施了一个应用内浏览器。我使用此activity在应用程序中打开新闻。我还使用此活动打开包含链接的推送通知。但是会出现以下问题:

  1. 我在ViewInWeb

  2. 中打开了应用程序中的链接
  3. 我按下主页按钮,以便应用程序进入后台

  4. 我收到一条包含链接的通知。

  5. 当我打开通知时,它不会显示此通知中的内容,但会显示我按下主页按钮之前的内容。

  6. 当我打开通知时,我使用以下代码:

                    Intent resultIntent;
                    PendingIntent resultPendingIntent = null;
    
                    resultIntent =new Intent(this, ViewInWeb.class);
                    Bundle basket = new Bundle();
                    basket.putString(TimeUtils.TAG_LINK,link );
                    basket.putString(TimeUtils.TAG_TITLE,title );
                    basket.putInt("TYPE", 1);
                    resultIntent.putExtras(basket);
    
                    resultPendingIntent = PendingIntent.getActivity(this, Integer.parseInt(nr),
                            resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    ViewInWeb的代码如下所示:

    public class ViewInWeb extends AppCompatActivity {
    
        private WebView webView;
        ProgressBar progressBar;
        private String link, title;
        private String url_link, url_title;
        private int count = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_in_web);
    
            webView = (WebView) findViewById(R.id.browser_web_view);
    
            // enable back button
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black);
            getSupportActionBar().setElevation(0);
    
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
    
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                   .
                   .
                //code for Google Analytics tracking
                   .
                    }
                    count++;
                }
            });
    
            //get the data
            Bundle basket_category = getIntent().getExtras();
            link = basket_category.getString(TimeUtils.TAG_LINK);
            title = basket_category.getString(TimeUtils.TAG_TITLE);
            int type = basket_category.getInt("TYPE");
            if(type==1){
    
                   .
                   .
                //code for Google Analytics tracking
                   .
            }
            url_link = link;
            url_title = title;
    
            //put the data
            getSupportActionBar().setTitle(title);
            webView.loadUrl(link);
    
            // create new ProgressBar and style it
            final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
    
            progressBar.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 25));
    
            // retrieve the top view of our application
            final FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
            decorView.addView(progressBar);
    
            ViewTreeObserver observer = progressBar.getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    View contentView = decorView.findViewById(android.R.id.content);
                    //progressBar.offsetTopAndBottom(contentView.getBottom()-10);
                    float x = contentView.getBottom();
    
                    int result = 0;
                    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                    if (resourceId > 0) {
                        result = getResources().getDimensionPixelSize(resourceId);
                    }
    
                    Log.e("aggbfaevfsa", String.valueOf(x));
                    progressBar.setY(contentView.getY() + result - 10);
                    // progressBar.setX(0);
    
                    ViewTreeObserver observer = progressBar.getViewTreeObserver();
                    observer.removeGlobalOnLayoutListener(this);
                }
            });
    
    
            webView.setWebChromeClient(new WebChromeClient() {
    
                // page loading progress, gone when fully loaded
                public void onProgressChanged(WebView view, int progress) {
                    if (progress < 100 && progressBar.getVisibility() == ProgressBar.GONE) {
                        progressBar.setVisibility(ProgressBar.VISIBLE);
                    }
                    progressBar.setProgress(progress);
                    if (progress == 100) {
                        progressBar.setVisibility(ProgressBar.GONE);
                    }
                }
    
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    super.onReceivedTitle(view, title);
                    if (!TextUtils.isEmpty(title)) {
                        getSupportActionBar().setTitle(title);
                        url_link = view.getUrl();
                        url_title = title;
                    }
                }
            });
            // set the animation when opening the activity
            overridePendingTransition(R.anim.activity_open_translate,
                    R.anim.activity_close_scale);
    
        }
    
        // set the animation when closing the activity
        @Override
        protected void onPause() {
            super.onPause();
            // closing transition animations
            overridePendingTransition(R.anim.activity_open_scale,
                    R.anim.activity_close_translate);
        }
    
    
    }
    

    当应用程序中有两个通知时,会出现同样的问题。打开第一个后,按下主页按钮。当我按下第二个时,它会显示第一个的内容。

    知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

问题是,在后台应用程序(它的活动可能尚未销毁)时,可能无法调用onCreate()

所以,您可以在ViewInWeb活动中执行以下操作,而不是在onCreate()中处理新意图:

public class ViewInWeb extends AppCompatActivity {
    ......
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         ....
         //initialize your widgets, as you did, but don't start loading data
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        handleIntent();
    }

    private void handleIntent() {
        // handle intent here!

        Bundle basket_category = getIntent().getExtras();
        link = basket_category.getString(TimeUtils.TAG_LINK);
        title = basket_category.getString(TimeUtils.TAG_TITLE);
        int type = basket_category.getInt("TYPE");
        if(type==1){
            //code for Google Analytics tracking
        }
        url_link = link;
        url_title = title;
        //put the data
        getSupportActionBar().setTitle(title);
        webView.loadUrl(link);
    }
}

NB!如果您使用片段,则建议使用protected void onResumeFragments()方法而非简单onResume()

处理意图

我希望,这有帮助