Siva Kumar
Siva Kumar

Reputation: 57

Open application when notification is clicked

Hi! I am having android code for simple push notification. Its working fine but when I am clicking on message in notification my application is not opening.

MainActivity

public class MainActivity extends Activity {
EditText ed1,ed2,ed3;
Notification notification;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ed1=(EditText)findViewById(R.id.editText);
    ed2=(EditText)findViewById(R.id.editText2);
    ed3=(EditText)findViewById(R.id.editText3);
    Button b1=(Button)findViewById(R.id.button);
    final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    b1.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {
            String tittle = ed1.getText().toString().trim();
            String subject = ed2.getText().toString().trim();
            String body = ed3.getText().toString().trim();

            Notification.Builder builder = new Notification.Builder(MainActivity.this);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
            Intent resultIntent = new Intent(String.valueOf(MainActivity.this));
            builder.setSmallIcon(R.drawable.icon)
                    .setContentIntent(pendingIntent)
                    .setContentTitle(tittle)
                    .setContentText(subject)
                    .setSubText(body);

            notification = builder.build();
            manager.notify(0, notification);
        }
    });
}

}

Help me to fix it

Upvotes: 1

Views: 4158

Answers (2)

user5615786
user5615786

Reputation:

Use Intent and and pass intent in pending intent.and also use PendingIntent.FLAG_UPDATE_CURRENT instead of 0 on Constructor of Pending intent.

 Intent myintent = new Intent(this, SplashScreen.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    myintent, PendingIntent.FLAG_UPDATE_CURRENT);

Upvotes: 4

Ravi
Ravi

Reputation: 35589

create object of Intent before creating PendingIntent

Intent resultIntent = new Intent(getApplicationContext(),MainActivity.this);

and then pass it to PendingIntent

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, 0);

Upvotes: 2

Related Questions