我有应用程序从服务器接收推送通知,并且工作正常。
但是没有用的是,如果我不打开通知,而下一个通知到达,则前一个丢失。

我想实现的是,我所有的通知都已保存,当我打开该活动时,我可以通过它们。

我设置了SharedPreferences和几个textViews,但是它们只显示我打开的最后一个。

我在考虑ListView,并创建一些本地库来保存传入的消息,但是我不知道应该朝哪个方向移动。

我还必须说,这对Android编程来说还是很新的东西,也许这是一些简单的东西,但我错过了。

对于代码的任何指导方针或建议,我将不胜感激

这是我的代码示例(来自GitHub,稍作修改):

package com.example.scratchtest;



import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.example.scratchtest.Splash.GlobalVariables;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class GreetingActivity extends Activity {
TextView emailET;
TextView emailET2;
TextView emailET3;
private AQuery aq;
// Progress Dialog bar object
ProgressDialog prgDialog;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
//  test za push//
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_greeting);
    aq = new AQuery(this);
    String json = getIntent().getStringExtra("greetjson");
    SharedPreferences prefs = getSharedPreferences("UserDetails",
            Context.MODE_PRIVATE);
    emailET = (TextView) findViewById(R.id.greetingmsg);
    emailET2 = (TextView) findViewById(R.id.greetingmsg2);
    emailET3 = (TextView) findViewById(R.id.greetingmsg3);

    // Check if Google Play Service is installed in Device
    // Play services is needed to handle GCM stuffs
    if (!checkPlayServices()) {
        Toast.makeText(
                getApplicationContext(),
                "This device doesn't support Play services, App will not work normally",
                Toast.LENGTH_LONG).show();
    }

    // When json is not null
    if (json != null) {
        try {

//              View ListView = findViewById(R.id.listView1);

            JSONObject jsonObj = new JSONObject(json);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("greetimgurl", jsonObj.getString("greetImgURL"));
            editor.putString("greetmsg", jsonObj.getString("greetMsg"));

            editor.commit();

            emailET.setText(prefs.getString("greetmsg", ""));

            // Render Image read from Image URL using aquery 'image' method
            aq.id(R.id.greetimg)
                    .progress(R.id.progress)
                    .image(prefs.getString("greetimgurl", ""), true, true,
                            0, 0, null, AQuery.FADE_IN);


            if (emailET.getText().toString().trim().length() == 0);
            emailET2.setText(prefs.getString("greetmsg", ""));


            if (emailET2.getText().toString().trim().length() == 0)

            emailET3.setText(prefs.getString("greetmsg", ""));


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    // When Json is null
    else if (!"".equals(prefs.getString("greetimgurl", ""))    && !"".equals(prefs.getString("greetmsg", "") != null)) {
        emailET.setText(prefs.getString("greetmsg", ""));
        aq.id(R.id.greetimg)
                .progress(R.id.progress)
                .image(prefs.getString("greetimgurl", ""), true, true, 0,
                        0, null, AQuery.FADE_IN);
    }
}

// Check if Google Playservices is installed in Device or not
private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    // When Play services not found in device
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            // Show Error dialog to install Play services
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    "This device doesn't support Play services, App will not work normally",
                    Toast.LENGTH_LONG).show();
            finish();
        }
        return false;
    } else {
        Toast.makeText(
                getApplicationContext(),
                "This device supports Play services, App will work normally",
                Toast.LENGTH_LONG).show();
    }
    return true;
}

// When Application is resumed, check for Play services support to make sure
// app will be running normally
@Override
protected void onResume() {
    super.onResume();
    checkPlayServices();
}


}

最佳答案

每当在客户端接收推送通知时(在服务中您正在处理所有内容)。您可以将其保存在Local SQLLiteDatabase或SharedPreferences中,并在需要时提取它们。

因此,每次您的邮件将被保存并且您可以进行相应的获取时,在通知栏上您可以显示最后收到的邮件。

如果您有更多有关推送通知的查询,可以使用GitHub Sample。请遵循ReadME文件中提到的所有必要集成步骤。

07-24 09:49
查看更多