Android 通知没有出现?

Android 通知没有出现?,android,android-notifications,Android,Android Notifications,这是我的通知生成器功能: public class NotificationHelper { final static String CHANNEL_ID = "MY_CHANNEL_ID"; final static String CHANNEL_NAME = "MY_CHANNEL_NAME"; public static Notification buildNotificationInstance(Context ctx, String title, String message)

这是我的通知生成器功能:

public class NotificationHelper {

final static String CHANNEL_ID = "MY_CHANNEL_ID";
final static String CHANNEL_NAME = "MY_CHANNEL_NAME";


public static Notification buildNotificationInstance(Context ctx, String title, String message) {

    Notification notification;
    NotificationCompat.Builder mBuilder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        mBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
    } else {
        mBuilder = new NotificationCompat.Builder(ctx);
    }

    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_android_black_24dp);

    Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher);
    mBuilder.setLargeIcon(bm);


    notification = mBuilder.build();


    return notification;
    }
}
我尝试在活动的onCreate()上显示通知:

通知没有显示。我做错了什么


我在Android 8上进行测试,您还必须为notificationBuilder添加PendingEvent,如下所示:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);

mBuilder.setContentTitle(title)
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_android_black_24dp)
        .setContentIntent(pendingIntent);

您必须创建通知通道才能使其工作: 试试这个:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        mBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);

        notificationChannel.setDescription("description");
        notificationChannel.enableLights(true);
        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }

为什么?当单击导航错误时,您还需要添加意图以使应用程序导航到您想要的活动。通知并非每次都用于打开活动。另外,它也不起作用:)是的,我知道,我的意思是如果你想导航到另一个活动,试试empulater或者8号以下的手机,我想在8号手机中你可能需要添加一些不同的东西,只是为了检查代码或安卓版本的问题安卓SDK的一致性和清晰性,一如往常。坦克兄弟。
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        mBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);

        notificationChannel.setDescription("description");
        notificationChannel.enableLights(true);
        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }