rohithdsouza

Notify Android Java

Sep 14th, 2021
1,035
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Notification;
  2. import android.app.NotificationManager;
  3. import android.app.PendingIntent;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. public class MainActivity extends AppCompatActivity
  11. {
  12.  Button notify;
  13.  EditText e;
  14.  @Override
  15.  protected void onCreate(Bundle savedInstanceState)
  16.  {
  17.  super.onCreate(savedInstanceState);
  18.  setContentView(R.layout.activity_main);
  19.  notify= (Button) findViewById(R.id.button);
  20.  e= (EditText) findViewById(R.id.editText);
  21.  notify.setOnClickListener(new View.OnClickListener()
  22. {
  23.  @Override
  24.  public void onClick(View v)
  25.  {
  26.  Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  27.  PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent,
  28. 0);
  29.  Notification noti = new
  30. Notification.Builder(MainActivity.this).setContentTitle("New
  31. Message").setContentText(e.getText().toString())
  32. .setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pending).build();
  33.  NotificationManager manager = (NotificationManager)
  34. getSystemService(NOTIFICATION_SERVICE);
  35.  noti.flags |= Notification.FLAG_AUTO_CANCEL;
  36.  manager.notify(0, noti);
  37.  }
  38.  });
  39.  }
  40. }
RAW Paste Data