直接上代码
:bundle是极光推送的bundle
@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;
}
通知的布局文件内容kongreveiver.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="6"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4.8"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black_cc"
android:layout_marginLeft="12dp"
android:text="天鑫运维"/>
<TextView
android:id="@+id/timemy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="sssss"
android:layout_marginRight="40dp"
android:textColor="@color/numtext" />
</LinearLayout>
<TextView
android:layout_marginLeft="12dp"
android:id="@+id/neirongte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="neir"
android:textColor="@color/bartoptec"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="2dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
系统通知
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