Simple implementation of Android Notification notification bar, click on the event, notice of suspension

Notification of the three elements

1. Small icons .setSmallIcon ()
2. Title .setContentTitle ()
3. Content .setContentText ()
This is a notification bar three elements of the three elements that you have to show up in the notification bar

Some commonly used methods
method effect
.setSmallIcon() Small Icons
.setContentTitle() title
.setContentText() content
.setDefaults(Notification.DEFAULT_ALL); Remind mode (ALL behalf of both sound vibration warning lamp)
.setLargeIcon() Large Icons
.setContentIntent() Click Jump
.setWhen() Notification time (when the notice)
.setAutoCancel(boolean) Whether to close Click notice
.setPriority(Notification.PRIORITY_MAX) Priority (MAX represents the most advanced) notice
API15 and use the following note

1 .java last piece tag .build () method requires replaced .getNotification ()
as follows: notificationManager.notify (0, builder.getNotification ());
2. .setPriority () methods are not available

Next is the complete implementation

java file

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("危险警报!!!")
                        .setContentText("您的手机已经被病毒入侵,点击紧急杀毒")
                        .setSmallIcon(R.mipmap.jiaojing)
  notificationManager.notify(0, builder.build()); //API15以及以下需要把.build()方法需要换成.getNotification()          
Notification of the click event

java code follows
simply instantiate a PendingIntent using .setContentIntent () method implementation

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
 PendingIntent  pi = PendingIntent.getActivity(context, 0, intent, 0);
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification.Builder builder = new Notification.Builder(TongZhiActivity.this);
                builder.setContentTitle("标题")
                        .setContentText("内容(点击跳转至百度)")
                        .setSmallIcon(R.mipmap.jiaojing)
                        .setContentIntent(pi)    
                        .setAutoCancel(true)    //点击后关闭通知
                        .setWhen(System.currentTimeMillis()) ;
                notificationManager.notify(1, builder.build());
Notification suspension notifications
 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
 PendingIntent  pi = PendingIntent.getActivity(context, 0, intent, 0);
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification.Builder builder = new Notification.Builder(TongZhiActivity.this);
                builder.setContentTitle("悬浮")
                        .setContentText("悬浮通知")
                        .setSmallIcon(R.mipmap.jiaojing)
                        .setContentIntent(pi)
                        .setAutoCancel(true)    //点击后关闭通知
                        .setWhen(System.currentTimeMillis())
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setFullScreenIntent(pi, true);
                notificationManager.notify(3, builder.build());
发布了6 篇原创文章 · 获赞 5 · 访问量 241

Guess you like

Origin blog.csdn.net/qq_44720366/article/details/103495205