Skip to content

Instantly share code, notes, and snippets.

@ammaratef45
Created April 14, 2017 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ammaratef45/b67bf42dd9611668dfb570cc828ba49a to your computer and use it in GitHub Desktop.
Save ammaratef45/b67bf42dd9611668dfb570cc828ba49a to your computer and use it in GitHub Desktop.
package com.tutorial.notification.ammar;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
Button ToastBut = (Button) findViewById(R.id.toastButton);
Button NotBut = (Button) findViewById(R.id.notiButton);
ToastBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "This is a toast message", Toast.LENGTH_SHORT).show();
}
});
NotBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
mBuilder.setSmallIcon(R.drawable.message);
mBuilder.setContentTitle("this is notification");
mBuilder.setContentText("This is the text you see in the notification");
Intent resultIntent = new Intent(getBaseContext(), MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment