发送一个普通的通知
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void notify(Context context, int id, String title,
String content, PendingIntent deletePendingIntent,
PendingIntent contentPendingIntent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// API16+
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_transparent)//不需要小图标,设置一个透明
.setShowWhen(false)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(contentPendingIntent)
.setAutoCancel(true) // 设置点击自动消失
.setSound( // 设置铃声默认设置
RingtoneManager.getActualDefaultRingtoneUri(
context, RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(
BitmapFactory.decodeResource(
context.getResources(),
R.drawable.icon)).build();
notificationManager.notify(id, notification);
} else {
// API11-15
NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(contentPendingIntent)
.setAutoCancel(true) // 设置点击自动消失
.setSound( // 设置铃声默认设置
RingtoneManager.getActualDefaultRingtoneUri(
context, RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.drawable.icon)
.setWhen(System.currentTimeMillis()).setOngoing(true);
Notification notification = builder.getNotification();
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
manager.notify(id, notification);
}
}
Intent intent = new Intent(mContext,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.
getActivity(mContext,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationUtils.notify(mContext,100,"标题","内容",pendingIntent,pendingIntent);
发送一个自定义界面的通知,使用广播来处理点击事件
public static void notifyCustomView(Context context, int id, PendingIntent deletePendingIntent,
PendingIntent contentPendingIntent,
RemoteViews remoteViews) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_ic)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(contentPendingIntent)
.setContent(remoteViews)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_ic))
.build();
notificationManager.notify(id, notification);
}
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setImageViewResource(R.id.notification_iv,R.drawable.ic_ic);
remoteViews.setTextViewText(R.id.notification_tv_title,"标题");
remoteViews.setTextViewText(R.id.notification_tv_content,"内容");
// 点击发送发送广播
Intent broadcastIntent = new Intent("com.ztt.action.click");
PendingIntent pendingIntentBroadcast = PendingIntent.getBroadcast(
mContext,100,broadcastIntent,PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notification_iv,pendingIntentBroadcast);
Intent intent = new Intent(mContext,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.
getActivity(mContext,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationUtils.notifyCustomView(mContext,100,pendingIntent,pendingIntent,remoteViews);