Notifaction是Android系统的的消息栏,
NotificationManager,然后实例化Notification,设置它的属性,通过NotificationManager
发出通知就可以了。基本步骤如下:
A.NotificationManarger
1.获取NotificationManarger
String service = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(service);
2.
Android O以下的话直接通过NotificationManarger.notify(int id, Notification notification)构建系统消息
Android O及以上版本还要构建消息渠道
NotificationChannel mChannel =new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(mChannel);
B.Notifaction的创建
1.Android O的创建
Notification notification =new Notification.Builder(context)
.setChannelId(id)
.setContentTitle("krcm110 new messages")
.setContentText("Success")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
2.Android O以下的版本创建
Notification notification =new Notification.Builder(context)
NotificationCompat.Builder notificationBuilder =new NotificationCompat.Builder(context)
.setChannelId(id)
.setContentTitle("krcm110 new messages")
.setContentText("Success")
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setShowWhen(true);
notification = notificationBuilder.build();
//....................................Code..............................................
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.example.krcm110.myapplication.R;
import com.example.krcm110.myapplication.app.view.activity.ActitityB;
import com.example.krcm110.myapplication.app.view.activity.MainActivity;
import com.example.krcm110.myapplication.com.service.ServiceBase;
import com.example.krcm110.myapplication.com.tools.LogUtil;
import java.util.ArrayList;
/**
*
* @author peter
* @date 2018/7/4
*/
public class Notificaitons {
public final static int NOTIFICATION_SAMPLE =0;
public final static StringACTION_SIMPLE ="com.android.peter.notificationdemo.ACTION_SIMPLE";
public final static StringACTION_ACTION ="com.android.peter.notificationdemo.ACTION_ACTION";
public final static StringACTION_YES ="com.android.peter.notificationdemo.ACTION_YES";
public final static StringACTION_NO ="com.android.peter.notificationdemo.ACTION_NO";
public final static StringACTION_DELETE ="com.android.peter.notificationdemo.ACTION_DELETE";
private static volatile NotificaitonssInstance =null;
private Notificaitons() {
}
public static Notificaitons getInstance() {
if(sInstance ==null) {
synchronized (Notificaitons.class) {
if(sInstance ==null) {
sInstance =new Notificaitons();
}
}
}
return sInstance;
}
// private NotificationManager notificationManager;
public void sendSimpleNotification(Context context,NotificationManager nm)
{
Intent intent =new Intent(context,ActitityB.class);
intent.setAction(ACTION_SIMPLE);
Intent intent1 =new Intent(context, MainActivity.class);
intent.setAction(ACTION_ACTION);
Intent[] aa =new Intent[2];
aa[0] = intent;
aa[1] = intent1;
PendingIntent pi = PendingIntent.getActivities(context,0,aa,0);
String id ="my_channel_01";
String name="我是渠道名字";
//notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification =null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel =new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
// Toast.makeText(context, mChannel.toString(), Toast.LENGTH_SHORT).show();
nm.createNotificationChannel(mChannel);
notification =new Notification.Builder(context)
.setChannelId(id)
.setContentTitle("5 new messages")
.setContentText("hahaha")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.setOngoing(true)
.setDeleteIntent(pi)
.build();
}else
{
notification =new NotificationCompat.Builder(context)
//设置渠道ID
.setChannelId(id)
.setContentTitle("5 new messages")
.setContentText("hahaha")
//设置小图标
.setSmallIcon(R.mipmap.ic_launcher)
//点击后自动消失
.setAutoCancel(true)
//屏蔽滑动关闭
//.setOngoing(true)
//设置点击通知时的响应事件
.setContentIntent(pi)
//设置删除通知时的响应事件
.setDeleteIntent(pi)
//显示时间
.setShowWhen(true)
.build();
//notification = notificationBuilder.build();
}
nm.notify(111123, notification);
}
//清除所有的通知
public void clearAllNotification(NotificationManager nm)
{
nm.cancelAll();
}
}