Alertdialog推送消息不会改变,总是第一个

时间:2014-07-24 16:41:12

标签: android android-alertdialog

这是我第一次发帖提问,我希望能做得好。我正在制作一个Android应用程序和我正在实现的事情之一是,当通知出现时,如果我在通知栏上选中,它会显示一条带有该消息的alertdialog,第一次有效,但是当我发送时其他消息,它始终显示发送它的第一条消息。

这是我在 GCMIntentService 上使用的代码:

private void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    varDb =new VSCSQLiteHelper(context, "Probe1", null, 1);

    String title = context.getString(R.string.app_name);

    if(tts == null) {
        tts = new TextToSpeech(this.getApplicationContext(), this);
    }

    Calendar c = Calendar.getInstance(); 
    hora = c.get(Calendar.HOUR_OF_DAY);

    db = varDb.getWritableDatabase();   
    config = db.rawQuery("SELECT * FROM Config_prob1",null); 
    config.moveToFirst();

    if(hora >= config.getInt(2) && hora <= config.getInt(3)){

        if(config.getInt(0) == 1 && config.getInt(1) == 1){
            notificationIntent = new Intent(context, Mensaje.class);   
            notificationIntent.putExtra("message", message);  
            tts.speak(message, TextToSpeech.QUEUE_ADD, null);

            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;

            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);

        }else if(config.getInt(1) == 1){
            notificationIntent = new Intent(context, Mensaje.class);   
            notificationIntent.putExtra("message", message);  
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;

            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);
        }else{
            tts.speak(message, TextToSpeech.QUEUE_ADD, null);   
        }
    }       
    config.close();
    db.close(); 
}

以及显示我所拥有的消息的类:

public class Mensaje extends Activity{

 private Bundle bundle = null;
 private String mensaje = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mensaje); 

    bundle = getIntent().getExtras();
    mensaje = bundle.getString("message");         

    AlertDialog.Builder builder = new AlertDialog.Builder(Mensaje.this);
    builder.setMessage(mensaje)
            .setTitle("Mensaje recibido")
            .setIcon(R.drawable.logo)
            .setCancelable(false)
            .setNeutralButton("Aceptar",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();                                   
                            Mensaje.this.finish();
                        }
                    });
    builder.create();
    builder.show();
}

@Override
public void onDestroy() {
    Log.i("Mensaje", "onDestroy()");
    super.onDestroy();
}
}

我尝试打印消息,de GCMIntent 上的消息很好,但在另一个类中始终是第一个消息。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

将您的PendingIntent更改为:

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

这样,您Intent将始终刷新您通知中提供的值。