다른 데이터로 일부 인텐트를 생성하더라도 알림에서 동일한 추가 데이터를 얻습니다. (I get the same extra data from notifications even if I create some intents with different data)


문제 설명

다른 데이터로 일부 인텐트를 생성하더라도 알림에서 동일한 추가 데이터를 얻습니다. (I get the same extra data from notifications even if I create some intents with different data)

I copied and paste from this document and tried to PutExtra.

I tapped button1button2 and button3, and then tapped a notification from button1, but ResultActivity was started with button3, why?

I want to be displayed as button1. Do you know a solution?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(listener);
        findViewById(R.id.button2).setOnClickListener(listener);
        findViewById(R.id.button3).setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    notifyNotification("button1");
                    break;
                case R.id.button2:
                    notifyNotification("button2");
                    break;
                case R.id.button3:
                    notifyNotification("button3");
                    break;
            }

        }
    };

    private int mId = 0;

    private void notifyNotification(String value) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.stat_notify_chat)
                        .setContentTitle("My notification")
                        .setContentText(value);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, ResultActivity.class);
        resultIntent.putExtra("value", value); // ***** I added this code *****

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(ResultActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

        mId++;
    }
}

public class ResultActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Intent intent = getIntent();
        String value = intent.getStringExtra("value");

        ((TextView)findViewById(R.id.textView)).setText(value);
    }
}

참조 솔루션

방법 1:

The problem lies on PendingIntent.FLAG_UPDATE_CURRENT and using same PendingIntent's request code, which is 0. That way, the all notifications' Intent will be updated to the last Button pressed.

Try to also use different request code for each PendingIntent.

PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        mId, // update to use same ID with notification's ID
        PendingIntent.FLAG_UPDATE_CURRENT
    );

방법 2:

long mId = System.currentTimemiles();

(by Yuki GotoAndrew T.Yogendra)

참조 문서

  1. I get the same extra data from notifications even if I create some intents with different data (CC BY‑SA 3.0/4.0)

#android-intent #android-pendingintent #android-notifications #Android






관련 질문

인텐트를 사용하여 메시지 작성 열기 (Opening create message using an intent)

다른 데이터로 일부 인텐트를 생성하더라도 알림에서 동일한 추가 데이터를 얻습니다. (I get the same extra data from notifications even if I create some intents with different data)

내 앱의 피드백 양식에 채워진 텍스트를 내 이메일 주소로 가져오는 방법은 무엇입니까? 자세한 내용을 참조하십시오 (How to get the text filled in feedback form of my app to my email address? Please see details)

Android 이미지 갤러리 이미지 자르기 및 ImageView 설정 (Android Image Gallery Crop Image and set ImageView)

xamarin에서 Android 활동 간에 데이터를 전달할 수 없습니다. (Can't pass data between android activities in xamarin)

모든 애플리케이션에 대한 Android 앱에서 데이터 공유 (Share Data in android app for all applications)

zxing이 브라우저에서 URL을 열도록 하거나 스캔 후 번호를 다이얼하는 방법 (How to make zxing open a URL in a browser or dial a number after scanning)

Android 암시적 의도 유효성 검사 (Android implicit intents validation)

kaldi 설치 시 libmkl_tbb_thread.so sth 관련 문제 (A problem related to libmkl_tbb_thread.so sth when installing kaldi)

Intent를 통해 데이터 전달 및 수신 (Passing data through Intent and receiving it)

android studio에서 인텐트를 통해 여러 콘텐츠를 보내는 방법 (how can i send multiple contents through intent in android studio)

Kotlin의 다른 EditText 값에 대한 의도 EditText 값 (Intent EditText values to another EditText values in Kotlin)







코멘트