1

最初はインテントが正常に機能するので、通知マネージャーで変数をインテントしていますが、2回目は新しいメッセージをインテントするときに、アクティビティが表示する新しいメッセージに大きな問題が発生しているのを助けてください

  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

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


}

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);
}
于 2014-05-27T11:28:49.267 に答える
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 に答える