团队开发冲刺第十一天

今天完成的是消息的推送提醒功能

  用到的是Notification的应用,这里我遇到的bug是,自己的手机就是不显示消息提醒。

明天继续解决这个bug,同时级那个头像框完成一下

package com.example.newbsh;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        Button button=findViewById(R.id.send_message);
        button.setOnClickListener(l);
    }
    View.OnClickListener l=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button:
                    sendNotification();
                    break;
            }
        }
    };
    private void sendNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = null;
        /**
         * 这里需要注意,8.0以上需要创建 Channel 渠道
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("测试渠道", getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(notificationChannel);
            builder = new NotificationCompat.Builder(this, "测试渠道");
        } else {
            builder = new NotificationCompat.Builder(this);
        }

        //Ticker是状态栏显示的提示
        builder.setTicker("显示setTicker");
        //第一行内容  通常作为通知栏标题
        builder.setContentTitle("显示setContentTitle");
        //第二行内容 通常是通知正文
        builder.setContentText("这里显示setContentText");
        //第三行内容 通常是内容摘要什么的 在低版本机器上不一定显示
        builder.setSubText("这里显示setSubText!");
        //ContentInfo 在通知的右侧 时间的下面 用来展示一些其他信息
        builder.setContentInfo("这里显示ContentInfo");
        //number设计用来显示同种通知的数量和ContentInfo的位置一样,如果设置了ContentInfo则number会被隐藏
        builder.setNumber(2);
        //true:点击通知栏,通知消失
        builder.setAutoCancel(true);
        //通知时间
        builder.setWhen(System.currentTimeMillis());
        //系统状态栏显示的小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //下拉显示的大图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setChannelId(this.getPackageName());//必须添加(Android 8.0) 【唯一标识
        Notification notification = builder.build();
        /**
         * 第一个参数为id,如果id相同则该通知会更新;
         */
        notificationManager.notify(1, notification);
    }
}

 

posted @ 2020-04-26 23:19  清风紫雪  阅读(163)  评论(0编辑  收藏  举报