Skip to content Skip to sidebar Skip to footer

Always Getting Data Null From Notification Intent Android

Always getting bundle null.... This is my code; edt=(EditText)findViewById(R.id.edt); String msg=edt.getText().toString(); //create Intent Intent myIntent = new In

Solution 1:

Get your data in onNewIntent(Intent intent) method.

@OverrideprotectedvoidonNewIntent(Intent intent) 
 {
   super.onNewIntent(intent);
   //code
 }

Solution 2:

I just solve my problem.....I defined Intent for SecondActivity out side the button click event .So, I got bundle blank.My code is look like this....

 edt=(EditText)findViewById(R.id.edt);

    //create Intent//Create listener to listen button clickOnClickListenerlistener=newOnClickListener() {
        publicvoidonClick(View view) {
            //Prepare Notification BuilderIntentmyIntent=newIntent(MainActivity.this, NotificationActivity.class);
            myIntent.putExtra("msg",edt.getText().toString());
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

            //Initialize PendingIntent
            pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);


            notificationBuilder = newNotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Notification Demo").
                            setSmallIcon(R.mipmap.ic_launcher).
                            setContentIntent(pendingIntent)
                    .setContentText(edt.getText().toString());
            //add soundUriuri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(uri);
            notificationBuilder .setWhen(System.currentTimeMillis());
            //vibratelong[] v = {500,1000};
            notificationBuilder.setVibrate(v);
            notificationManager.notify(1, notificationBuilder.build());
        }
    };
    Buttonbtn= (Button)findViewById(R.id.button);
    btn.setOnClickListener(listener);

Post a Comment for "Always Getting Data Null From Notification Intent Android"