Уведомление не отображается в устройствах Samsung Samsung - PullRequest
0 голосов
/ 07 марта 2019

Я столкнулся со странной проблемой сегодня, push-уведомление не отображается на устройствах Samsung, в которых есть oreo, но оно нормально работает на других устройствах oreo.Вот код, который я использую:

 private void sendNotification(String messageTitle,String messageBody) {

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder=null;
        try {
        Intent intent = new Intent(this, SplashActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        String NOTIFICATION_CHANNEL="NB Shop Notification";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .setSound(defaultSoundUri)
                .setChannelId(NOTIFICATION_CHANNEL)
                .setContentIntent(pendingIntent);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            /* Create or update. */
            NotificationChannel channel;
            channel=new NotificationChannel(NOTIFICATION_CHANNEL,
                    NOTIFICATION_CHANNEL,
                    NotificationManager.IMPORTANCE_DEFAULT);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
            notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
        }catch (Exception e)
        {
            Log.d("sendNotification",e.toString());
        }
    }

Это не показывает ошибки в журнале.

1 Ответ

1 голос
/ 07 марта 2019

Я обнаружил проблему, вызвавшую проблему, конструктор (NotificationCompat.Builder(this)) NotificationCompat.Builder, имеющий только один параметр, устарел, Чтобы избавиться от устаревания, мы должны отправить идентификатор канала с помощью конструктора, поэтому я изменил свой код и передал идентификатор канала в конструктор следующим образом:

NotificationCompat.Builder(this,NOTIFICATION_CHANNEL)

также удалено

.setChannelId(NOTIFICATION_CHANNEL)

и теперь этот код также работает на устройствах Samsung.

...