直接上代码
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
。。。。。。
try {
RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.kongreveiver);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String time = getTime();
// String hhmm = timetodate(time);
customView.setTextViewText(R.id.timemy, time);
if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_TITLE))) {
customView.setTextViewText(R.id.name, bundle.getString(JPushInterface.EXTRA_TITLE));
}
String ones = bundle.getString(JPushInterface.EXTRA_MESSAGE);
JSONObject jsonObject = new JSONObject(ones);
String title = jsonObject.getString("title");
String type = jsonObject.getString("type");
String content = jsonObject.getString("content");
customView.setTextViewText(R.id.neirongte, title + ":" + content);
Intent intentCancel = new Intent(context, NotificationBroadcastReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, 0,
intentCancel, PendingIntent.FLAG_ONE_SHOT);
Intent companyIntroduce = new Intent(context, TwoMainActivity.class);
companyIntroduce.putExtra("type", type);
// companyIntroduce.putExtra("name", name);
int notifyId = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder//设置通知栏标题
// .setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE)) //设置通知栏显示内容
.setContent(customView)
.setContentIntent(pendingIntent) //设置通知栏点击意图
.setDeleteIntent(pendingIntentCancel)//取消消息回调
// .setTicker(context.getPackageName() + "消息")//通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
//.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
.setDefaults(Notification.DEFAULT_SOUND)
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.mipmap.txlogo);
// mNotificationManager.notify(notifyId, notify);
// Log.i("lgqq","body=====id============"+tzid+"....."+ones);
int num = ShareUtil.getSharedInt("num");
num++;
ShareUtil.sharedPint("num", num);
mNotificationManager.notify(num, mBuilder.build());
} catch (JSONException e) {
e.printStackTrace();
}
public String getTime() {
long currentTime = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分");
Date date = new Date(currentTime);
System.out.println(formatter.format(date));
// long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
String str = String.valueOf(formatter.format(date));
return str;
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(APPlocation.getTotalContext());
NotificationManager mNotificationManager = (NotificationManager) APPlocation.getTotalContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"com.tianxin.service2thread",
TAG,
NotificationManager.IMPORTANCE_DEFAULT
);
mNotificationManager.createNotificationChannel(channel);
}
mBuilder.setChannelId("com.tianxin.service2thread");//当前包名
mBuilder.setContentText("内容");
Intent notificationIntent = new Intent(this, TwoActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mNotificationManager.notify(1, mBuilder.build());
Android 8.0系统不出通知解决方法:
https://blog.csdn.net/meixi_android/article/details/83379335