android8.0以上版本区别于android7.0通知的差距是,以上的版本必须添加渠道NotificationChannel
创建通知之前需要对android版本进行一个判断

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)

activity_main.xml代码里仅有一个Button用于响应通知,代码不再展示
MainActivity.java代码如下:

public class MainActivity extends AppCompatActivity {    private Button GetNotification;    private static final int ID = 1;    private static final String CHANNELID ="1";    private static final String CHANNELNAME = "channel1";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        GetNotification = (Button) findViewById(R.id.GetNotification);        GetNotification.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);               // manager.cancel(1);                //安卓8.0以上弹出通知需要添加渠道NotificationChannel                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){                    //创建渠道                    /**                     * importance:用于表示渠道的重要程度。这可以控制发布到此频道的中断通知的方式。                     * 有以下6种重要性,是NotificationManager的静态常量,依次递增:                     * IMPORTANCE_UNSPECIFIED(值为-1)意味着用户没有表达重要性的价值。此值用于保留偏好设置,不应与实际通知关联。                     * IMPORTANCE_NONE(值为0)不重要的通知:不会在阴影中显示。                     * IMPORTANCE_MIN(值为1)最低通知重要性:只显示在阴影下,低于折叠。这不应该与Service.startForeground一起使用,因为前台服务应该是用户关心的事情,所以它没有语义意义来将其通知标记为最低重要性。如果您从Android版本O开始执行此操作,系统将显示有关您的应用在后台运行的更高优先级通知。                     * IMPORTANCE_LOW(值为2)低通知重要性:无处不在,但不侵入视觉。                     * IMPORTANCE_DEFAULT (值为3):默认通知重要性:随处显示,产生噪音,但不会在视觉上侵入。                     * IMPORTANCE_HIGH(值为4)更高的通知重要性:随处显示,造成噪音和窥视。可以使用全屏的Intent。                     */                    NotificationChannel channel = new NotificationChannel(CHANNELID,CHANNELNAME,NotificationManager.IMPORTANCE_HIGH);                    manager.createNotificationChannel(channel);//开启渠道                    Intent intent = new Intent(MainActivity.this,notification.class);                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);                    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,CHANNELID);                    builder .setContentTitle("Title")//通知标题                            .setContentText("ContentText")//通知内容                            .setWhen(System.currentTimeMillis())//通知显示时间                            .setContentIntent(pendingIntent)                            .setSmallIcon(R.drawable.smile)                            .setAutoCancel(true)//点击通知取消                            //.setSound()                            //第一个参数为手机静止时间,第二个参数为手机震动时间,周而复始                            .setVibrate(new long[] {0,1000,1000,1000})//手机震动                            //第一个参数为LED等颜色,第二个参数为亮的时长,第三个参数为灭的时长                            .setLights(Color.BLUE,1000,1000)                            /**表示通知的重要程度                             * RIORITY_DEFAULT                             * RIORITY_MIN                             * RIORITY_LOW                             * RIORITY_HIGE                             * RIORITY_MAX                            **/                            .setPriority(NotificationCompat.PRIORITY_MAX)                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile)).build();                    manager.notify(1,builder.build());                } else{                Notification notification = new NotificationCompat.Builder(MainActivity.this)                        .setContentTitle("Title")                        .setContentText("ContentText")                        .setWhen(System.currentTimeMillis())                        .setSmallIcon(R.drawable.smile)                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile))                        .build();                manager.notify(1,notification);            }       }        });    }}

我们还需要创建一个活动notification,用于响应点击通知后执行的操作,此处就使用了一个TextView显示一段文本
如果要设置手机震动还得在清单文件中声明权限

 

效果图如下:
我们点击按钮,通知出现

点击通知跳转到notification活动

因为我们设置了 .setAutoCancel(true),所有点击通知后,通知消失

更多相关文章

  1. android设定手机的显示模式,横竖屏,是否全屏
  2. Android(安卓)LiveCD VirtualBox
  3. Android学习笔记(二三): 多页显示-Flipper的使用
  4. android EditText提示居右无法显示的问题
  5. Android设置显示文本
  6. android:weight的使用
  7. android横屏竖屏
  8. Android(安卓)TextView实现跑马灯效果
  9. 【进阶篇】Android学习笔记――TextInputLayout

随机推荐

  1. unity发布android流程
  2. 如何运用百度地图SDK进行开发(一)初级篇
  3. Android AsyncTask 初探
  4. Android(安卓)通过DownloadManager实现自
  5. Android五个进程等级
  6. 【android】初学安卓,简单布局和activity
  7. Android的animation(动画)
  8. 弄错一个概念:Android的Thread与Handler是
  9. Android(安卓)ArrayList LinkedList Set
  10. 分享第一本中文Android书籍(应用框架和程