0

I have an application,I am sending notification with this code

private void notBuild() {
    int mId=1;
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentTitle("Message")
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.twitter)
            .setContentText("user01 sent a message")
            .setNumber(15);
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);

    // 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(MainActivity.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());
}

This is starting mainactivity and it's okay.But I want to run a method in the main activity.

Example:You will see in the setContentText user01 sent a message,when user press the notification I want to run startChat(user01) method in the main activity.How can I do it ?

Serkay Sarıman
  • 465
  • 1
  • 10
  • 17

1 Answers1

0

You can put data in the resultIntent variable.

resultIntent.putExtra('key', 'useridhere');

Then, in your Activity (onCreate or OnResume) you can call

String userId = getIntent().getStringExtra('key', null);
if(userId != null) 
{ 
   //StartChat(....);
}
kevskree
  • 4,442
  • 3
  • 24
  • 32