Android学习笔记8-使用通知(Notification)

Android学习笔记8-使用通知(Notification)

1.Notification简介

通知是Android系统的一种特色的功能,当某个app希望给用户提示信息,但是该app又不在运行在前台时,就可以利用通知。
发送一条通知后,手机上方的状态栏就会显示一个小图标,下拉状态栏,会显示通知的具体信息。

实现代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button) findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.send_notice:
                String CHANNEL_ID = "my_channel_01";
                CharSequence name = getString(R.string.channel_01);
                String description = "你好世界";
                int importance = NotificationManager.IMPORTANCE_LOW;
                
                NotificationManager manager = (NotificationManager) getSystemService
                        (NOTIFICATION_SERVICE);
                
				//Android 8.0开始需要为通知设置channel
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
                channel.setDescription(description);
                channel.enableLights(true);
                channel.setLightColor(Color.RED);
                channel.enableVibration(true);
                channel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});

				manager.createNotificationChannel(channel);
                Notification notification = new Notification.Builder(this)
                		//设置通知的标题
                        .setContentTitle("This is content title") 
                        // 设置通知的详细信息
                        .setContentText("打到皇家马德里") 
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher ))
                        .setChannelId(CHANNEL_ID)
                        .build();
                manager.notify(1,notification );
                break;
            default:break;
        }
    }
}

**从Android O开始 每个通知必须为其设置对应的channel **

channel的构造函数有三个参数 channel的id,name,和优先级。 创建完channel后通过 NotificationManager的createNotificationChannel(channel)方法添加chanel,并在在notification里设置setChannelId。

通常 通知会在广播接收器(broadcastReciver)或者服务(Server)中创建,不在活动中创建,因为app给我们发送通知的时候并不是运行在前台。

通过点击通知跳转活动

如果我们想要用户点击app发出的通知有反应,比如进入进入一个页面,这里就需要用到pendingIntent.
PendingIntent和Intent很类似,都可以用于启动活动,启动服务,以及发送广播。但是pendingIntent更倾向于某个合适的时机执行某个动作,相当于延迟类型的Intent.
                //设置pendingIntent让用户点击通知时跳转活动。
                Intent intent = new Intent(this,NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this,0 ,intent ,0 );

获取PendingIntent实例可以通过PendingIntent的getActivity(),getBroadCast(),getService()方法
第一个参数是Context,第二个参数一般为0,第三个是Intent对象,第四个是确定pendingIntent的行为,有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCLE_CURRENT,FLAG_UPDATE_CURRENT四种值,一般传入0

最后通过NotificationCompat的setContentIntent(pi)加上点击功能。

                Notification notification = new NotificationCompat.Builder(this)
                      .setContentTitle("This is content title")
                      .setContentText("打到皇家马德里")
                      .setWhen(System.currentTimeMillis())
                      .setSmallIcon(R.mipmap.ic_launcher)
                      .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher ))
                      .setChannelId(CHANNEL_ID)
                      .setContentIntent(pi)
                      .build();

设置当用户点击完通知操作后,通知图标就消失

第一种方法

当点击了通知后,通知会自动取消掉。

第二种方法,在跳转的活动里设置
public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }
}

这里manager.cancel()传入的参数是给每条参数指定的id.

2.Notification进阶技巧


需要声明权限

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

也可以直接默认设置

3.Notification的高级技巧

设置通知栏详情的长文本内容

显示大图片

设置通知的重要程度

  • PRIORITY_DEFAULT 默认的重要程度
  • PRIORITY_MIN 表示最低的重要程度,系统可能只会在特定的场景才会显示这条通知
  • PRIORITY_LOW 表示较低的重要程度
  • PRIORITY_HIGH 表示较高的重要程度,系统可能会将这类通知放大,或改变其显示顺序,比较靠前的位置
  • PRIORITY_MAX 最高的重要程度,必须让用户立刻看到,甚至做出相应的操作。

猜你喜欢

转载自blog.csdn.net/cyyyy_da/article/details/82938613
今日推荐