自定义Layout带按钮
private void popup() {
//自定义布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
//Button按钮
Intent intent = new Intent("test");
intent.putExtra("click", "您点击了Button");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_click, pendingIntent);
//Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setOngoing(false);
builder.setAutoCancel(false);
// builder.setContent(remoteViews);
builder.setSmallIcon(R.mipmap.ic_launcher);
//Notification
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.bigContentView = remoteViews;
//NotificationManager
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//唤醒通知
manager.notify(0, notification);
}
自定义Layout不带按钮
public void showNotification(SmsData data) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
mBuilder.setSmallIcon(R.drawable.sms_notificantion);
// mBuilder.setContentTitle("通知的标题");
// mBuilder.setContentText("通知的内容");
Notification notification = mBuilder.build();
// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
// contentView.setImageViewResource(R.id.iv_sms_notification, R.drawable.sms_notificantion);
contentView.setTextViewText(R.id.tv_notification_name, getContactsName(data));
contentView.setTextViewText(R.id.tv_notification_address, data.address);
notification.bigContentView = contentView;
// 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
// 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
// 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
// 点击notification自动消失(注意:此处若使用FLAG_NO_CLEAR则上面的notification.bigContentView = contentView失效)
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 通知的时间
notification.when = System.currentTimeMillis();
// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(context, SmsActivity.class);
// 当用户点击通知栏的Notification时候,切换回MainActivity。
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
/**
* flags有四个取值:
* int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
*int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
*int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
*int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
*我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。
*/
notification.contentIntent = pi;
// 发送到手机的通知栏
notificationManager.notify(0, notification);
}