Skip to content

Commit 407bc2d

Browse files
committed
Show basic notification
1 parent c42406c commit 407bc2d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

app/src/main/java/io/github/adamjodlowski/notifications/MainActivity.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package io.github.adamjodlowski.notifications;
22

3+
import android.app.NotificationManager;
34
import android.os.Bundle;
5+
import android.support.v4.app.NotificationCompat;
46
import android.support.v7.app.AppCompatActivity;
57
import android.view.View;
68
import android.widget.Button;
79

810
public class MainActivity extends AppCompatActivity {
911

1012
private Button button;
13+
private int notificationId = 1;
1114

1215
@Override
1316
protected void onCreate(Bundle savedInstanceState) {
@@ -20,7 +23,18 @@ protected void onCreate(Bundle savedInstanceState) {
2023
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
2124
@Override
2225
public void onClick(View view) {
23-
// TODO display notification
26+
27+
// NotificationCompat Builder takes care of backwards compatibility and
28+
// provides clean API to create rich notifications
29+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
30+
.setSmallIcon(android.R.drawable.ic_dialog_info)
31+
.setContentTitle("Notification title")
32+
.setContentText("Content text");
33+
34+
// Obtain NotificationManager system service in order to show the notification
35+
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
36+
notificationManager.notify(notificationId, mBuilder.build());
37+
2438
}
2539
};
2640
}

0 commit comments

Comments
 (0)