【问题标题】:Why does not show notification on Android 9为什么在 Android 9 上不显示通知
【发布时间】:2019-03-09 18:54:23
【问题描述】:

在 Android 9.0 上不显示通知,在 8 及更低版本上运行良好。 我在使用旧代码时遇到了问题,我读到需要在新的 android 版本上通过 Channel,我从互联网上获得了此代码,但仍然无法在 Android 9.0 上运行。我不知道为什么。 任何帮助将不胜感激。

当我通过互联网阅读时,一切看起来都很好。但没有显示通知。

    private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
        String CHANNEL_ID = Constants.ChannelID;
        String CHANNEL_NAME = "Notification";

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);;
        NotificationCompat.Builder notification;
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.enableVibration(true);
            channel.setLightColor(Color.BLUE);
            channel.enableLights(true);
            channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notification),
                    new AudioAttributes.Builder()
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .build());
            channel.canShowBadge();
            notificationManager.createNotificationChannel(channel);
            notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            notification = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
        }
        notification
                .setVibrate(new long[]{0, 100})
                .setPriority(Notification.PRIORITY_MAX)
                .setLights(Color.BLUE, 3000, 3000)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setWhen(getTimeMilliSec(timeStamp))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(inboxStyle)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
                .setContentText(message)
                .build();

        notificationManager.notify(CHANNEL_ID, 1, notification.build());
    }

【问题讨论】:

    标签: android notifications android-notifications android-9.0-pie notificationmanager


    【解决方案1】:

    我认为问题在于您没有在notificationBuilder 上使用setChannelId()。 (我冒昧地将notification 重命名为notificationBuilder,因为它就是这样。)

    我删除了其中一行代码中多余的分号。

    还要注意canShowBadge()是一个返回值的类方法,但不设置任何通知设置。我将其替换为setShowBadge(true)

    请阅读我在代码片段中留下的 cmets 以获得更多信息。

    private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
            String CHANNEL_ID = Constants.ChannelID;
            String CHANNEL_NAME = "Notification";
    
            // I removed one of the semi-colons in the next line of code
            NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    
            inboxStyle.addLine(message);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                channel.enableVibration(true);
                channel.setLightColor(Color.BLUE);
                channel.enableLights(true);
                channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notification),
                        new AudioAttributes.Builder()
                                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                                .build());
                //channel.canShowBadge();
                // Did you mean to set the property to enable Show Badge?
                channel.setShowBadge(true);
                notificationManager.createNotificationChannel(channel);
            }
    
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                .setVibrate(new long[]{0, 100})
                .setPriority(Notification.PRIORITY_MAX)
                .setLights(Color.BLUE, 3000, 3000)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setWhen(getTimeMilliSec(timeStamp))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(inboxStyle)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
                .setContentText(message);
            // Removed .build() since you use it below...no need to build it twice
    
            // Don't forget to set the ChannelID!!
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                notificationBuilder.setChannelId(ID_SPECIAL_OFFER_NOTIFICATION);
            }
    
            notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
        }
    

    【讨论】:

    • 抱歉回复晚了,但仍然没有显示通知。
    • 您在代码中的适当位置添加了“setChannelId”?
    • @Web.11,我面临同样的问题。你是怎么解决的?
    • @Barns,我尝试了您建议的解决方案,还有什么可以尝试的吗?
    • @Keselme 抱歉。我没有要测试的 Android 9.0 设备。我在其他设备上对上述代码没有任何问题。祝你好运#
    【解决方案2】:

    是的,这已经回答了,但需要帮助

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int returnFlag = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createNotificationChannel();
                Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText("Mytrux Running")
                        .setAutoCancel(true);
                Notification notification = builder.build();
                startForeground(NOTIFICATION_ID, notification);
                returnFlag = START_NOT_STICKY;
            } else {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText("Mytrux is Running...")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setAutoCancel(true);
                Notification notification = builder.build();
                startForeground(NOTIFICATION_ID, notification);
                returnFlag = START_STICKY;
            }
            mHandlerTask.run();
            return returnFlag;
        }
    

    并添加此方法

    private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel serviceChannel = new NotificationChannel(
                        ANDROID_CHANNEL_ID,
                        "Foreground Service Channel",
                        NotificationManager.IMPORTANCE_DEFAULT
                );
    
                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(serviceChannel);
            }
        }
    

    不要忘记在清单中添加服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2021-12-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多