1NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(yourContext.getApplicationContext(), "notify_001");
2Intent ii = new Intent(yourContext.getApplicationContext(), YourMainActivty.class);
3PendingIntent pendingIntent = PendingIntent.getActivity(yourContext, 0, ii, 0);
4
5NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
6bigText.bigText(notificationsTextDetailMode); //detail mode is the "expanded" notification
7bigText.setBigContentTitle(notificationTitleDetailMode);
8bigText.setSummaryText(usuallyAppVersionOrNumberOfNotifications); //small text under notification
9
10mBuilder.setContentIntent(pendingIntent);
11mBuilder.setSmallIcon(R.mipmap.ic_launcher); //notification icon
12mBuilder.setContentTitle(notificationTitle); //main title
13mBuilder.setContentText(notificationText); //main text when you "haven't expanded" the notification yet
14mBuilder.setPriority(Notification.PRIORITY_MAX);
15mBuilder.setStyle(bigText);
16
17NotificationManager mNotificationManager = (NotificationManager) yourContext.getSystemService(Context.NOTIFICATION_SERVICE);
18
19NotificationChannel channel = new NotificationChannel("notify_001",
20 "Channel human readable title",
21 NotificationManager.IMPORTANCE_DEFAULT);
22if (mNotificationManager != null) {
23 mNotificationManager.createNotificationChannel(channel);
24}
25
26if (mNotificationManager != null) {
27 mNotificationManager.notify(0, mBuilder.build());
28}
1package com.example.notificationdemo;
2
3import android.app.Activity;
4import android.app.NotificationManager;
5import android.app.PendingIntent;
6import android.content.Context;
7import android.content.Intent;
8import android.support.v4.app.NotificationCompat;
9import android.os.Bundle;
10import android.view.View;
11import android.widget.Button;
12
13public class MainActivity extends Activity {
14 Button b1;
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 b1 = (Button)findViewById(R.id.button);
21 b1.setOnClickListener(new View.OnClickListener() {
22 @Override
23 public void onClick(View v) {
24 addNotification();
25 }
26 });
27 }
28
29 private void addNotification() {
30 NotificationCompat.Builder builder =
31 new NotificationCompat.Builder(this)
32 .setSmallIcon(R.drawable.abc)
33 .setContentTitle("Notifications Example")
34 .setContentText("This is a test notification");
35
36 Intent notificationIntent = new Intent(this, MainActivity.class);
37 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
38 PendingIntent.FLAG_UPDATE_CURRENT);
39 builder.setContentIntent(contentIntent);
40
41 // Add as notification
42 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
43 manager.notify(0, builder.build());
44 }
45}