Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

图标变为默认图标的解决方案 #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
调整逻辑,所有通知可在8.0以上正常使用
KellyZong committed May 31, 2019
commit 7a6d1222406e585694dcfcc34abf4af1a4065c17
65 changes: 30 additions & 35 deletions library/src/main/java/com/wenming/library/NotifyUtil.java
Original file line number Diff line number Diff line change
@@ -36,6 +36,19 @@ public NotifyUtil(Context context, int ID) {
// 获取系统服务来初始化对象
nm = (NotificationManager) mContext.getSystemService(Activity.NOTIFICATION_SERVICE);
cBuilder = new Notification.Builder(mContext);

if (Build.VERSION.SDK_INT >= 26) {
//当sdk版本大于26
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
channel.enableLights(true);
channel.enableVibration(true);
nm.createNotificationChannel(channel);
cBuilder = new Notification.Builder(mContext, id);
cBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}
}

/**
@@ -62,25 +75,25 @@ private void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String
cBuilder.setContentText(content);// 设置通知中心中的内容
cBuilder.setWhen(System.currentTimeMillis());

/*
/*
* 将AutoCancel设为true后,当你点击通知栏的notification后,它会自动被取消消失,
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
cBuilder.setAutoCancel(true);
// 将Ongoing设为true 那么notification将不能滑动删除
// notifyBuilder.setOngoing(true);
/*
* 从Android4.1开始,可以通过以下方法,设置notification的优先级,
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
cBuilder.setPriority(Notification.PRIORITY_MAX);
/*
* Notification.DEFAULT_ALL:铃声、闪光、震动均系统默认。
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
int defaults = 0;

if (sound) {
@@ -116,7 +129,7 @@ private void setBuilder(PendingIntent pendingIntent, int smallIcon, String ticke

nBuilder.setTicker(ticker);
nBuilder.setWhen(System.currentTimeMillis());
nBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
nBuilder.setPriority(Notification.PRIORITY_MAX);

int defaults = 0;

@@ -176,7 +189,6 @@ public void notify_mailbox(PendingIntent pendingIntent, int smallIcon, int large
PendingIntent deletePendingIntent = PendingIntent.getService(mContext,
deleteCode, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
cBuilder.setDeleteIntent(deletePendingIntent);

**/

Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);
@@ -226,19 +238,16 @@ public void notify_customview(RemoteViews remoteViews, PendingIntent pendingInte
* @param title
* @param content
*/
public void notify_normail_moreline(PendingIntent pendingIntent, int smallIcon, String ticker,
public void notify_normal_multiline(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, boolean sound, boolean vibrate, boolean lights) {

final int sdk = Build.VERSION.SDK_INT;
if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
notify_normal_singline(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
Toast.makeText(mContext, "您的手机低于Android 4.1.2,不支持多行通知显示!!", Toast.LENGTH_SHORT).show();
} else {
setBuilder(pendingIntent, smallIcon, ticker, true, true, false);
nBuilder.setContentTitle(title);
nBuilder.setContentText(content);
nBuilder.setPriority(Notification.PRIORITY_HIGH);
notification = new Notification.BigTextStyle(nBuilder).bigText(content).build();
setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
notification = new Notification.BigTextStyle(cBuilder).bigText(content).build();
// 发送该通知
nm.notify(NOTIFICATION_ID, notification);
}
@@ -259,8 +268,8 @@ public void notify_progress(PendingIntent pendingIntent, int smallIcon,
setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
/*
* 因为进度条要实时更新通知栏也就说要不断的发送新的提示,所以这里不建议开启通知声音。
* 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。
*/
* 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。
*/

new Thread(new Runnable() {
@Override
@@ -295,26 +304,12 @@ public void run() {
*/
public void notify_bigPic(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, int bigPic, boolean sound, boolean vibrate, boolean lights) {

if(Build.VERSION.SDK_INT >= 26) {
//当sdk版本大于26
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
// channel.enableLights(true);
// channel.enableVibration(true);
nm.createNotificationChannel(channel);
cBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}

setCompatBuilder(pendingIntent, smallIcon, ticker, title, null, sound, vibrate, lights);
Notification.BigPictureStyle picStyle = new Notification.BigPictureStyle();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
bigPic, options);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), bigPic, options);
picStyle.bigPicture(bitmap);
picStyle.bigLargeIcon(bitmap);
cBuilder.setContentText(content);
Original file line number Diff line number Diff line change
@@ -75,8 +75,7 @@ private void initListView() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
// notify_normal_singLine();
notiTest();
notify_normal_singLine();
mScaleImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.image1));
break;
case 1:
@@ -202,7 +201,7 @@ private void notify_normal_moreLine() {
String content = "据台湾“中央社”报道,国民党主席朱立伦今天(18日)向中常会报告,为败选请辞党主席一职,他感谢各位中常委的指教包容,也宣布未来党务工作由副主席黄敏惠暂代,完成未来所有补选工作。";
//实例化工具类,并且调用接口
NotifyUtil notify2 = new NotifyUtil(mContext, 2);
notify2.notify_normail_moreline(pIntent, smallIcon, ticker, title, content, true, true, false);
notify2.notify_normal_multiline(pIntent, smallIcon, ticker, title, content, true, true, false);
currentNotify = notify2;
}

@@ -252,7 +251,10 @@ private void notify_bigPic() {
currentNotify = notify4;
}

private void notiTest() {
/**
* 此方法可作为简单测试使用
*/
private void notifyTest() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
NotificationManager manager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
@@ -272,7 +274,7 @@ private void notiTest() {

String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(id, description, importance);
channel.enableLights(true);
channel.enableVibration(true);