首先,发送一个状态栏通知必须用到两个类:NotificationManager、Notification。 NotificationManager:是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager是一个系统Service,必须通过getSystemService()方法来获取。 

NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
Notification:是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。 

1、直接上代码

开启相关权限

                                        
  NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);        Notification.Builder builder1 = new Notification.Builder(Welcome.this);        builder1.setSmallIcon(R.drawable.ic_launcher); //设置图标        builder1.setTicker("显示第二个通知");        builder1.setContentTitle("第一个通知"); //设置标题        builder1.setContentText("点击查看详细内容"); //消息内容        builder1.setWhen(System.currentTimeMillis()); //发送时间        builder1.setDefaults(Notification.DEFAULT_SOUND);//设置指示灯        builder1.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动        builder1.setLights(Color.GREEN, 1000, 2000);        builder1.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));        builder1.setSmallIcon(R.drawable.ic_launcher);        builder1.setAutoCancel(true);//打开程序后图标消失        Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.music);        builder1.setSound(path);        Intent intent = new Intent(Welcome.this, NotificationClickReceiver.class);        PendingIntent pendingIntent = PendingIntent.getBroadcast(Welcome.this, 0, intent, 0);        builder1.setContentIntent(pendingIntent);        Notification notification1 = builder1.build();        notificationManager.notify(124, notification1); // 通过通知管理器发送通知

监听消息点击后的触发事件:首先创建NotificationClickReceiver类并继承BroadcastReceiver类

package com.example.online.retailers;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * * Created by Waitforyou on 2019-03-26. */public class NotificationClickReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //todo 跳转之前要处理的逻辑        Log.i("TAG", "userClick:我被点击啦!!! ");        Intent newIntent = new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(newIntent);    }}

2、悬挂式通知

//在 build()之前设置 .setFullScreenIntent()   Notification builder = new NotificationCompat.Builder(Context);    Notification notify = builder.setSmallIcon(R.mipmap.ic_launcher_round)            .setPriority(Notification.PRIORITY_DEFAULT)  //通知的优先级            .setCategory(Notification.CATEGORY_MESSAGE)  //通知的类型            .setContentTitle("通知")            .setAutoCancel(true)            .setContentIntent(pi)            .setContentText("Heads - Up Notification on Android 5.0")            .setFullScreenIntent(pi, true)  //不设置此项不会悬挂,false 不会出现悬挂            .build();

3、折叠式通知

Notification builder = new NotificationCompat.Builder(Context);    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.sina.com"));    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);    // 未下拉的样式 R.layout.collapsed    RemoteViews collapsed = new RemoteViews(getPackageName(), R.layout.collapsed);    collapsed.setTextViewText(R.id.collapsed_text, "关闭状态");    //下拉后的样式R.layout.show    RemoteViews show = new RemoteViews(getPackageName(), R.layout.show);    Notification notify = builder.setAutoCancel(true)            .setSmallIcon(R.mipmap.ic_launcher_round)            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))            .setContentIntent(pi)            .setContentText("新浪微博")            .setCustomContentView(collapsed)//下拉前            .setCustomBigContentView(show)//下拉后            .build();    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    manager.notify(0, notify);

4.锁屏通知

//通过 setVisibility() 方法设置即可....setVisibility(VISIBILITY_PUBLIC).build();

 

更多相关文章

  1. Android(安卓)应用开发笔记 - 切换图片(ImageSwitcher)
  2. Android(安卓)自定义控件,模仿小米秒表样式的时钟,完整代码注解
  3. android添加常驻图标到状态栏
  4. Android(安卓)N NotificationManagerService源码分析
  5. Android沉浸式通知栏
  6. android中给用户提醒的三种方式
  7. 【Android】利用服务Service创建标题栏通知
  8. 《Android(安卓)UI基础教程》之读书笔记
  9. Android跳转到系统通知管理页面

随机推荐

  1. ANDROID: NDK编程入门笔记
  2. 想看看Android L的效果,结果导致Eclispe无
  3. android:gravity="start" Android 多行文
  4. ANDROID音频系统散记之一:A2dpAudioInterf
  5. android 每周学习笔记及总结(每周更新)
  6. Android 中 WebView 使用漏洞相关介绍
  7. Dagger 2 在 Android 上的使用(一)
  8. android遇到的BUG
  9. 什么是android(What Is Android?)
  10. Android(安卓)JNI入门第五篇——基本数据