Skip to content

Instantly share code, notes, and snippets.

@dylanwatsonsoftware
Last active December 14, 2015 23:39
Show Gist options
  • Save dylanwatsonsoftware/5167135 to your computer and use it in GitHub Desktop.
Save dylanwatsonsoftware/5167135 to your computer and use it in GitHub Desktop.
A simple method to create an android notification
private void fireNotification(int score) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Hit!")
.setContentText("High Score!")
.setNumber(score)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, LLRActivity.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(MyActivity.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.
int mId = 1;
mNotificationManager.notify(mId, mBuilder.build());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment