Android 9.0 NotificationManager 不显示,不通知,具体解决方法如下
Android 8.0以上(必须,必须,必须)
Notification.Builder notification = new Notification
.Builder(applicationContext)
.setContentTitle("您有一条新消息")
.setContentText(title)
.setWhen(System.currentTimeMillis())
.setSmallIcon(idIco) //必须添加(Android 8.0)
.setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), idIco))
.setVibrate(vibrate)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setChannelId(applicationContext.getPackageName()) //必须添加(Android 8.0) 【唯一标识】
.setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
但是呢,Android9.0你试试用上边的分分钟给你不显示,气得你牙直发痒
Android 9.0以上(必须,必须,必须)
NotificationChannel channel = new NotificationChannel(
applicationContext.getPackageName(),
"会话类型",//这块Android9.0分类的比较完整,你创建多个这样的东西,你可以在设置里边显示那个或者第几个
NotificationManager.IMPORTANCE_DEFAULT
);
全部代码(显示工具类)
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.MediaStore;
import androidx.core.app.NotificationCompat;
import cn.handhi.im.ui.MainActivity;
public class NotificationManagerUtils {
//开始通知
@SuppressLint("WrongConstant")
public static void startNotificationManager(String title, int idIco){
XUIUtils.Companion.runUiOnThread(new Runnable() {
@Override
public void run() {
Context applicationContext = XUIUtils.Companion.getApplicationContext();
NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(applicationContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, 0);
long [] vibrate = {0, 500, 1000, 1500};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder notification = new Notification
.Builder(applicationContext)
.setContentTitle("您有一条新消息")
.setContentText(title)
.setWhen(System.currentTimeMillis())
.setSmallIcon(idIco)
.setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), idIco))
.setVibrate(vibrate)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setChannelId(applicationContext.getPackageName())
.setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
NotificationChannel channel = new NotificationChannel(
applicationContext.getPackageName(),
"会话消息(掌嗨)",
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(0,notification.build());
XUIUtils.Companion.show(title);
}else{
Notification.Builder notification = new Notification
.Builder(applicationContext)
.setContentTitle("您有一条新消息")
.setContentText(title)
.setWhen(System.currentTimeMillis())
.setSmallIcon(idIco)
.setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), idIco))
.setVibrate(vibrate)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
notificationManager.notify(0,notification.build());
}
}
});
}
}
最后一种方法,通用高版本[服务中设置]
private static final String NOTIFICATION_CHANNEL_ID = "自己随便设置(必须是英文)";
private Notification buildNotification(String title, String msg) {
final Resources res = getResources();
final String contentTitle = title;
final String contentText = msg;
// final String intentAction = mVisibleWindow ? ACTION_HIDE : ACTION_SHOW;
// Intent actionIntent = new Intent(this, TermuxFloatService.class).setAction(intentAction);
Notification.Builder builder = new Notification.Builder(this).setContentTitle(contentTitle).setContentText(contentText)
.setPriority(Notification.PRIORITY_MIN).setSmallIcon(R.mipmap.ic_launcher)
// .setColor(0xFF000000)
//.setContentIntent(PendingIntent.getService(this, 0, actionIntent, 0))
.setOngoing(false)
.setShowWhen(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannel();
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
//final int messageId = mVisibleWindow ? R.string.toggle_hide : R.string.toggle_show;
//builder.addAction(android.R.drawable.ic_menu_preferences, res.getString(messageId), PendingIntent.getService(this, 0, actionIntent, 0));
return builder.build();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupNotificationChannel() {
String channelName = "Termux_Backer";
String channelDescription = "Notifications from Termux_Backer";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
channel.setDescription(channelDescription);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}
//调用
int NOTIFICATION_ID = 555;//自己随便设置
//在服务中调用
startForeground(NOTIFICATION_ID, buildNotification("标题","内容"));
//Activity中调用[不能直接调用,必须启动服务调用]
startForegroundService();
网友评论