1

我在通知管理器中意图一个变量,因为意图第一次成功工作,但第二次当我意图新消息时,活动显示出危险的价值请帮助我解决我真的遇到了一个大问题

  1. 这是通知管理器的代码
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. 在接收消息类中
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());
    }
}

提前致谢

4

4 回答 4

7

替换此行

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

PendingIntent contentIntent = contentIntent = PendingIntent.getActivity(context,
                    (int) (Math.random() * 100), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
于 2013-10-16T12:32:20.793 回答
4

在第 #1 节中,您要添加此值:

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

在接收消息类中(顺便说一下,命名错误,类名应该是驼峰式)你有:

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

也许你应该在那里:

saveit = i.getStringExtra("activate");

这个想法是,从您发布的内容来看,尚不清楚是否有任何组件实际上run额外提供了此意图字符串。

编辑使用您的代码,以便从活动触发上述通知管理器:

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);
    }

}

和一个MySoActivity类的结果:

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);
    }

}

通知很好,当点击通知时,我得到了预期的值。代码中的唯一区别是我使用的是getApplicationContext()而不是您上面的上下文,但我不确定这有多相关。也许你可以比较差异,看看你做错了什么......

于 2013-10-16T12:31:50.557 回答
0

在您的 generatenotification() 方法中使用以下代码:

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


    }

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

不要使用 0。

希望这可以帮助。

于 2013-10-16T12:40:19.003 回答
0

这段代码对我有用,试试这个。

替换此代码:

在 GCMIntentService.java 中替换此“generateNotification”方法。

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


}

接收消息.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);
}
于 2014-05-27T11:28:49.267 回答