android中使用通知功能

本文实现一个功能:点击一个按钮,发送一个系统通知功能

添加一个Activity

 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main36Activity">

    <Button
        android:id="@+id/button40"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示通知" />
</android.support.constraint.ConstraintLayout>

 

 

MainActivity.java:

 1 package com.example.chenrui.app1;
 2 
 3 import android.app.Notification;
 4 import android.app.NotificationChannel;
 5 import android.app.NotificationManager;
 6 import android.graphics.BitmapFactory;
 7 import android.os.Build;
 8 import android.support.v4.app.NotificationCompat;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.widget.Button;
13 
14 public class MainActivity extends AppCompatActivity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20 
21         Button button = findViewById(R.id.button1);
22         button.setOnClickListener(new View.OnClickListener() {
23             @Override
24                 public void onClick(View v) {
25                 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
26 
27                 String channelId = "app1";
28                 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
29                     NotificationChannel channel = new NotificationChannel(channelId,"app1",NotificationManager.IMPORTANCE_DEFAULT);
30                     manager.createNotificationChannel(channel);
31                 }
32 
33                 Notification notification = new NotificationCompat.Builder(MainActivity.this,channelId)
34                         .setContentTitle("通知标题")
35                         .setContentText("通知正文")
36                         .setWhen(System.currentTimeMillis())
37                         .setSmallIcon(R.mipmap.ic_launcher)
38                         .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
39                         .build();
40                 manager.notify(1,notification);
41             }
42         });
43     }
44 }

 

注意第28-31行的代码,Android8.0及以上版本,要发送通知,需要配置通知频道,不然无法成功发送通知。

上面的通知,在点击的时候,不会有任何反应,下面我们把代码修改一下,允许在点击的时候进入一个Activity活动,注意红色字体为新增的代码:

 1 package com.example.chenrui.app1;
 2 
 3 import android.app.Notification;
 4 import android.app.NotificationChannel;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Intent;
 8 import android.graphics.BitmapFactory;
 9 import android.os.Build;
10 import android.support.v4.app.NotificationCompat;
11 import android.support.v7.app.AppCompatActivity;
12 import android.os.Bundle;
13 import android.view.View;
14 import android.widget.Button;
15 
16 public class MainActivity extends AppCompatActivity {
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22 
23         Button button = findViewById(R.id.button1);
24         button.setOnClickListener(new View.OnClickListener() {
25             @Override
26                 public void onClick(View v) {
27                 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
28 
29                 String channelId = "app1";
30                 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
31                     NotificationChannel channel = new NotificationChannel(channelId,"app1",NotificationManager.IMPORTANCE_DEFAULT);
32                     manager.createNotificationChannel(channel);
33                 }
34 
35                 Intent intent = new Intent(MainActivity.this,Main2Activity.class);
36                 PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
37 
38                 Notification notification = new NotificationCompat.Builder(Main2Activity.this,channelId)
39                         .setContentTitle("通知标题")
40                         .setContentText("通知正文")
41                         .setWhen(System.currentTimeMillis())
42                         .setSmallIcon(R.mipmap.ic_launcher)
43                         .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
44                         .setContentIntent(pi)
45                         .setAutoCancel(true)
46                         .build();
47                 manager.notify(1,notification);
48             }
49         });
50     }
51 }

PendingIntent可以理解为一种延时执行的Intent,当点击通知项的时候进入到对应的Activity中。

 

更多属性的使用:

.setDefaults(NotificationCompat.DEFAULT_ALL)

使用通知的默认效果,这个属性会根据当前手机的环境决定播放什么铃声,以及如何震动等

 

.setPriority(NotificationCompat.PRIORITY_MAX)

设置通知的优先级,设置为NotificationCompat.PRIORITY_MAX是最高级,会以横幅的方式显示通知,部分手机可能禁用了横幅通知,需要手工开启

 

.setStyle(new NotificationCompat.BigTextStyle().bigText("通知内容"))

如果内容过多的话,默认会显示成省略号,如果要显示大文本信息,可以使用setStyle,用于显示大文本

posted @ 2019-01-18 14:47  魔豆  阅读(1738)  评论(0编辑  收藏  举报