I am Intenting an variable in the notification manager as first time the intent works successfully but second time when i intent the new message the activity shows thr perivous value plz help me out i m really in a big problem

  1. here is the code of notification manager
public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {

        Log.i(TAG, "Device registered: regId = " + registrationId);

        displayMessage(context, "Your device registred with GCM");

        Log.d("NAME", MainActivity.name);

        ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {

        Log.i(TAG, "Device unregistered");

        displayMessage(context, getString(R.string.gcm_unregistered));

        ServerUtilities.unregister(context, registrationId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {

        Log.i(TAG, "Received message");

        String message = intent.getExtras().getString("price");

        displayMessage(context, message);

        // notifies user

        generateNotification(context, message);

    }

    @Override
    protected void onDeletedMessages(Context context, int total) {

        Log.i(TAG, "Received deleted messages notification");

        String message = getString(R.string.gcm_deleted, total);

        displayMessage(context, message);

        // notifies user

        generateNotification(context, message);

    }


    @Override
    public void onError(Context context, String errorId) {

        Log.i(TAG, "Received error: " + errorId);

        displayMessage(context, getString(R.string.gcm_error, errorId));

    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {

        // log message

        Log.i(TAG, "Received recoverable error: " + errorId);

        displayMessage(context, getString(R.string.gcm_recoverable_error,
                errorId));

        return super.onRecoverableError(context, errorId);
    }


    private static void generateNotification(Context context, String message) {

        int icon = R.drawable.orange_logo;

        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = new Notification(icon, message, when);

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

        Intent notificationIntent = new Intent(context,receivemessage.class);

        // set intent so it does not start a new activity

        notificationIntent.putExtra("activate",message.toString());

        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, contentIntent);

        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);     
    }
}
  1. In receivemessage class
public class receivemessage extends Activity{

    TextView textshow;
    String saveit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);

        textshow=(TextView)findViewById(R.id.showmessage);

        Intent i=getIntent();
        saveit = i.getStringExtra("run");
        textshow.setText(saveit.toString());
    }
}

Thanks in advance

有帮助吗?

解决方案

Replace this line

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

with

PendingIntent contentIntent = contentIntent = PendingIntent.getActivity(context,
                    (int) (Math.random() * 100), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

其他提示

In section #1 you're adding this value:

notificationIntent.putExtra("activate",message.toString());

while in receivemessage class (by the way, bad naming, class names should be camel case) you're having:

Intent i=getIntent();
saveit = i.getStringExtra("run");

maybe you should have there:

saveit = i.getStringExtra("activate");

The idea is that from what you have posted, it is not clear if any component is actually providing this run intent string extra.

EDIT Using your code so that the above notification manager is triggered from an activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_some_action).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setupClick();
            }
        });
    }

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

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

        Intent notificationIntent = new Intent(this, MySoActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.putExtra("run", message.toString());
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(this.getApplicationContext(), 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);
    }

}

and the result in a MySoActivity class:

public class MySoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_so_layout);
        TextView lblIntentExtra = (TextView) findViewById(R.id.lblIntentExtra);
        Intent intent = getIntent();
        String value = intent.getStringExtra("run");
        if (TextUtils.isEmpty(value)) {
            value = "NONE@!";
        }
        lblIntentExtra.setText(value);
    }

}

The notification is put alright and when tapping on the notification I am getting the expected value. The only difference in the code is that I am using getApplicationContext() instead of the context you're having above, but I am not sure how relevant that is. Maybe you can compare the differences and see where you're doing wrong ...

In your generatenotification() method use below code :

    private void generateNotification(Context context, String message, String query) {

    int icon = R.drawable.icon;
        long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification;

    Intent intent = new Intent(context, myActivity.class);


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(appname).setWhen(when)
                    .setAutoCancel(true).setContentTitle(appname)
                    .setContentText(message).build();

            notificationManager.notify((int) when, notification);


    }

Jus see : notificationManager.notify((int) when, notification);

Dont use 0.

Hope this helps.

This code working for me, try this.

Replace this code :

in GCMIntentService.java replace this "generateNotification" method.

GCMIntentService.java

private static 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);

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

    Intent notificationIntent = new Intent(context, NotificationReceiver.class);

    notificationIntent.putExtra("Notice", message);
    notificationIntent.setAction(Intent.ACTION_VIEW);
    notificationIntent.setAction("myString"+when);
    notificationIntent.setData((Uri.parse("mystring"+when)));

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent intent = PendingIntent.getActivity(context, (int) when, 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((int) when, notification);


}

Receivemessage.java

TextView textshow;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message);

    textshow=(TextView)findViewById(R.id.showmessage);   

    Intent in = getIntent();         
    String text = in.getStringExtra("Notice");
    textshow.setText(text);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top