Skip to content

Instantly share code, notes, and snippets.

@ardovic
Created June 3, 2019 00:11
Show Gist options
  • Select an option

  • Save ardovic/4cf5fcf6c4a41d4de13cf96b136c4334 to your computer and use it in GitHub Desktop.

Select an option

Save ardovic/4cf5fcf6c4a41d4de13cf96b136c4334 to your computer and use it in GitHub Desktop.
Sample notification implementation (Android Activity)
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class SampleNotificationActivity extends AppCompatActivity {
Button bttn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, SampleNotificationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
bttn = (Button) findViewById(R.id.bttn);
bttn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(SampleNotificationActivity.this)
.setSmallIcon(android.R.drawable.stat_notify_error)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("This is the notification title")
.setContentText("This is the notification content")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(SampleNotificationActivity.this);
notificationManager.notify(1, notificationBuilder.build());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment