Android系统。通知显示在状态栏中,但不显示或发出声音。 api 24

问题描述 投票:0回答:1

我有一个推送通知进入应用程序。然而在api 24中,我显示的通知仅出现在状态栏中,但它不显示(如弹出窗口),并且它不会发出任何声音。

我无法弄清楚为什么......

这是我的代码:

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationId = 1;
    String channelId = "channel-03";
    String channelName = "Channel Name";
    int importance = 0;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        Log.d(TAG, "android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N");
        importance = NotificationManager.IMPORTANCE_HIGH;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Log.d(TAG, "android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O");
        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(icon)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    Intent intent1 = new Intent(context, MainActivity.class);

    stackBuilder.addNextIntent(intent1);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);


    notificationManager.notify(notificationId, mBuilder.build());
android notifications
1个回答
0
投票

需要添加到构建器:

.setDefaults(Notification.DEFAULT_ALL)

© www.soinside.com 2019 - 2024. All rights reserved.