繁体   English   中英

Android:屏幕上不显示通知,只显示状态栏Oreo中的图标

[英]Android: The notification does not appear on the screen, only the icon in the status bar Oreo

您好我写了一个类来创建通知,但问题是状态栏中出现通知图标,通知块没有出现甚至在锁定屏幕中。 在doc中,它被编写为使用NotificationManagerCompat来显示通知( Docs链接 )。 这是我的代码:

public class NotificationsService {
    private Context mContext;
    private NotificationManager mNotificationManager;

    public NotificationsService(Context context) {
        mContext = context;
    }

    public void sendNotification(int iconResource) {
        Notification notification = buildNotification(iconResource);
//        mNotificationManager.notify(0 /* ID of notification */, notification);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
        notificationManager.notify(70, notification);
    }

    public Notification buildNotification(int iconResource) {
        String CHANNEL_ID = "type"; // The id of the channel.
        CharSequence channelName = "dsfsf";

        NotificationChannel channel = null;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
                .setSmallIcon(iconResource)
                .setContentTitle("title")
                .setContentText("message")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(defaultSoundUri)
                .setChannelId(CHANNEL_ID);
//                .setContentIntent(pendingIntent);

        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }

        return notificationBuilder.build();
    }

并使用这样:

NotificationsService notif = new NotificationsService(context);
  notif.sendNotification(R.mipmap.ic_launcher);

仅显示应用程序图标,并且仅在状态栏中显示通知

在此输入图像描述

您需要通知通知管理员您的通知。 我开发了一种为Oreo或上层SDK生成通知的方法。

在代码中使用正确的参数调用此方法...

//This method is for Oreo
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void createSimpleNotificationForOreo(Context context, PendingIntent pendingIntent, String notificationTitle, String notificationContent, int notificationId, String notificationChannelId) {
        Notification.BigTextStyle bigText = new Notification.BigTextStyle();
        bigText.bigText(notificationContent);
        bigText.setBigContentTitle(notificationTitle);
        Notification.Builder notificationBuilder = new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_small_notification)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setContentText(notificationContent)
                .setStyle(bigText)
                .setChannelId(notificationChannelId)
                .setContentIntent(pendingIntent);

        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = new NotificationChannel(notificationChannelId, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(notificationId, notification);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM