Skip to content

Instantly share code, notes, and snippets.

@pablophg
Created January 20, 2015 09:04
Show Gist options
  • Save pablophg/5bd23cbc32cf6dbd2b99 to your computer and use it in GitHub Desktop.
Save pablophg/5bd23cbc32cf6dbd2b99 to your computer and use it in GitHub Desktop.
Notification example with auto-cancel
package net.pablophg.notifications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
public static final int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Iniciamos el manager de notificaciones
NotificationManager myNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Llamada al activity que se abrira al pulsar la notification
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
// Variables de la notificacion
CharSequence NotificationTitle = "Notification";
CharSequence NotificationContent = "Notification test";
CharSequence line1 = "First line on the notification";
CharSequence line2 = "Second line on the notification";
CharSequence resume = "+3 more";
// Imagen para la notificacion
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// Obtenemos la notification configurada en el builder
Notification notification = new Notification.InboxStyle(
new Notification.Builder(getApplicationContext())
.setContentTitle(NotificationTitle)
.setAutoCancel(true) // Cancels the notification after user clickson it
.setContentText(NotificationContent)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bm))
.addLine(line1)
.addLine(line2)
.setSummaryText(resume)
.build();
myNotificationManager.notify(NOTIFICATION_ID, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment