1、自定义一个带按钮的Notification布局:layout_notification;
2、创建Notification
RemoteViews views = new RemoteViews(getPackageName(),R.layout.layout_nitification); //自定义的布局视图
//按钮点击事件:
PendingIntent homeIntent = PengdingIntent.getBroadcast(this,1,new Intent("action"),PengdingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.btn,homeIntent); //点击的id,点击事件
//创建通知:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext);
mBuilder.setContent(views) //设置布局
.setOngoing(true) //设置是否常驻,true为常驻
.setSmallIcon(R.mipmap.ic_laucher) //设置小图标
.setTicker("通知来了") //设置提示
.setPriority(Notification.PRIORITY_MAX) //设置优先级
.setWhen(System.currentTimeMillis()) //设置展示时间
.setContentIntent(PendingIntent.getBroadcast(this,2,new Intent("action.view"),PengingIntent.FLAG_UPDATE_CURRENT); //设置视图点击事件
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(100,mBuilder.build()) //显示通知: 当前notificationid,当前notification
3、创建NotificationBroadcast接收通知
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); //动作
collapseStatusBar(context); //收起通知栏
Intent i = new Intent(context,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //必须添加,避免重复打开
if (action.equals("com.xczl.smart.notification.home")){
i.putExtra("flag","home"); //传值
context.startActivity(i);
}
}
public void collapseStatusBar(Context context) {
try {
Object statusBarManager = context.getSystemService("statusbar");
Method collapse;
if (Build.VERSION.SDK_INT <= 16) {
collapse = statusBarManager.getClass().getMethod("collapse");
} else {
collapse = statusBarManager.getClass().getMethod("collapsePanels");
}
collapse.invoke(statusBarManager);
} catch (Exception localException) {
localException.printStackTrace();
}
}
4、配置清单
MainActivity:设置LanchMode为singleTask
注册Broadcast:
配置通知栏伸缩权限:
5、MainActivity接收消息
//覆写 onNewIntent()
@Override
protected void onNewIntent(Intent intent) {
String extra = intent.getStringExtra("flag");
if (!TextUtils.isEmpty(extra)){
if (extra.equals("home")){
//接收到值的具体操作
}
}
}
代码示例:
a、类GlobalDeclare
public class GlobalDeclare { //notification点击广播 public final static String ACTION_NOTIFICATION_CLICK = "com.ACTION_NOTIFICATION_CLICK"; }
b、类NotifyClickReceiver
/** * 全局广播 * Created by xl on 2018/5/9. */ public class NotifyClickReceiver extends BroadcastReceiver { private final static String TAG = NotifyClickReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(GlobalDeclare.ACTION_NOTIFICATION_CLICK.equals(action)){ MyLogger.i(TAG, "broadcast"); } } }
c、清单文件声明
android:name=".NotifyClickReceiver">
android:name="com.ACTION_NOTIFICATION_CLICK" />
d、showNotification函数
public void showNotification(Context context, String title, String msg) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setAutoCancel(true);//点击后消失 builder.setSmallIcon(R.mipmap.ic_launcher);//设置通知栏消息标题的头像 builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置通知铃声,震动,闪光灯 builder.setTicker(context.getText(R.string.app_name)); builder.setContentTitle(title); builder.setContentText(msg); //Intent intent = new Intent();//只显示通知,无页面跳转 Intent intent = new Intent(GlobalDeclare.ACTION_NOTIFICATION_CLICK);//将要跳转的界面 //intent.setAction(GlobalDeclare.ACTION_NOTIFICATION_CLICK); //Intent intent = new Intent(context, NotifyClickReceiver.class);//将要跳转的界面 //利用PendingIntent来包装我们的intent对象,使其延迟跳转 int id = (int) (System.currentTimeMillis() / 1000); PendingIntent intentPend = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(intentPend); NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); manager.notify(0, builder.build()); }完!!!