我有应用程序从服务器收到推送通知,并且工作正常。 但是无效的是,如果我不打开通知,并且下一个通知到达,则先前丢失。
我想实现,我的所有通知都已保存,当我打开活动时,我可以通过它们。
我设置了SharedPreferences和几个textView,但它们只显示我打开的最后一个。
我正在考虑ListView,并创建一些本地基础,我可以保存传入的消息,但我不知道我应该朝哪个方向移动。
我还必须说这对于Android编程来说还是一个新手,也许这是一些简单的东西,但我很想念它。
我会感谢任何guidlines或代码的核心
以下是我的代码示例(来自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();
}
}
答案 0 :(得分:1)
每当推送通知在客户端(在服务中处理所有内容)时收到。您可以在Local SQLLiteDatabase或SharedPreferences中保存它,并在需要时获取它们。
因此,每当您的消息保存并且您可以相应地获取消息并在通知栏上显示最后一条消息。
如果您对推送通知有更多疑问,可以使用GitHub Sample。请按照自述文件中提到的所有必要步骤进行整合。