У меня есть приложение на Android, которое отображает последние новости. Когда есть большая история или новости, я отправляю уведомление в приложение с помощью GCM. Но у меня проблема. Сценарий этой проблемы, как это:Когда уведомления открываются, он отображает содержимое предыдущего уведомления
- Я посылаю уведомления о первой новости
- пользователь не хочет, чтобы открыть его, чтобы он пойло его
- Через некоторое время отправить еще уведомления о другая новость, отличная от первой
- Когда пользователь открывает ее, она отображает первую новость, а не вторую, правую.
код, который я использовал, как показано ниже:
public class GCMNotificationIntentService extends IntentService {
// Sets an ID for the notification, so it can be updated
public static final int notifyID = 9001;
JSONArray array_add = null;
NotificationCompat.Builder builder;
// object helper to store the notification configuration
private SharedPreferenceNotification shared_pref;
String type, content, nr, title, link, subtype;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification("[" + extras.get(ApplicationConstants.MSG_KEY)
+ "]");
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
try {
array_add = new JSONArray(msg);
try {
for (int i = 0; i < array_add.length(); i++) {
JSONObject c = array_add.getJSONObject(i);
type = c.getString(Utils.NOT_TYPE);
content = "[" + c.getString(Utils.NOT_CONTENT) + "]";
JSONArray content_a = new JSONArray(content);
for (int j = 0; j < content_a.length(); j++) {
JSONObject b = content_a.getJSONObject(j);
title = b.getString(Utils.NOT_TITLE);
nr = b.getString(Utils.NOT_NR);
link = b.getString(Utils.NOT_LINK);
subtype = b.getString(Utils.NOT_SUBTYPE);
}
}
} catch (Exception e) {
}
} catch (Exception e) {
}
Tracker t = GoogleAnalytics.getInstance(this)
.newTracker("UA-2983962-14");
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory("Notifications")
.setAction(type)
.setLabel("Dergimi")
.build());
// initialize shared preference manager
shared_pref = new SharedPreferenceNotification(this);
if (shared_pref.getValue(type)) {
Intent resultIntent;
PendingIntent resultPendingIntent = null;
if (type.equalsIgnoreCase("news"){
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);
t.send(new HitBuilders.EventBuilder()
.setCategory("Notifications")
.setAction(type)
.setLabel(title)
.build());
resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_ONE_SHOT);
displayNot(resultPendingIntent);
Log.e("Erdhi ketu", "Erdhi ketuttttttt");
}
}
}
private void displayNot(PendingIntent resultPendingIntent){
MyApplication mApplication = (MyApplication) getApplicationContext();
mApplication.getTracker(MyApplication.TrackerName.APP_TRACKER);
NotificationCompat.Builder mNotifyBuilder;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("AppName").setContentText(title)
.setSmallIcon(R.drawable.ic_launcher);
// Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText(title);
// Set autocancel
mNotifyBuilder.setAutoCancel(true);
// Post a notification
mNotificationManager.notify(Integer.valueOf(nr),
mNotifyBuilder.build());
}
Любая идея, что может пойти не так?
set В ожидании Intent PendingIntent.FLAG_UPDATE_CURRENT –
Да, это работает, но если пользователь еще не открыл уведомление, значит, есть два уведомления, первый принимает содержимое второго – Xhulio
, если пользователь не открывает уведомление и 2 уведомление приходит, то предыдущее уведомление обновляется, или вы можете сказать, что перезаписываете это. –