Android уведомление не отображается в API 29 - PullRequest
1 голос
/ 28 апреля 2020

У меня проблема с отображением уведомления на Android API 29. Хотя код основан на этом учебном пособии, при выполнении кода уведомление не отображается.

MainActivity .kt:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ...

        val notification =
            NotificationCompat.Builder(this, NOTIFICATION_GROUP_LOCATION)
                .setContentTitle("NoteIfication")
                .setContentText("Note:  Priority: ")
                .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build()
        val channel = NotificationChannel(
            NOTIFICATION_CHANNEL_LOCATION,
            "group location",
            NotificationManager.IMPORTANCE_DEFAULT
        ).apply {
            description = "notification channel for note reminders in app HyperNote"
        }

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)

        with(NotificationManagerCompat.from(this)) {
            notify(2134, notification)
        }
    }

}

Ответы [ 2 ]

1 голос
/ 01 мая 2020

Как отметил Майк М.: «Похоже, вы могли использовать два разных идентификатора канала; NOTIFICATION_GROUP_LOCATION и NOTIFICATION_CHANNEL_LOCATION. Идентификатор, который вы передаете в NotificationCompat.Builder (this, ...), должен совпадать с передан в NotificationChannel (..., "местоположение группы", IMPORTANCE_DEFAULT). "

После изменения идентификаторов было показано уведомление.

0 голосов
/ 28 апреля 2020

Это метод, который я использую для локальных уведомлений со дня 1

 private void sendNotification(Blog blog) {
        String title=blog.getTitle();
        String desc=blog.getDesc();
        String img=blog.getImage();
        String time =blog.getTimestamp();

        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("desc", desc);
        bundle.putString("image", img);
        bundle.putString("time", time);

        Intent intent = new Intent(this, NavigationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("blog", bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_TAG,
                intent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            URL url = new URL(img);
            Notification notification = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo_icon)
                    .setContentTitle(title)
                    .setContentText(desc)
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(BitmapFactory.decodeStream((InputStream)url.getContent()))
                            .bigLargeIcon(null))
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent)
                    .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "OlaitexBlog notifications",
                        NotificationManager.IMPORTANCE_DEFAULT);
                Objects.requireNonNull(notificationManager).createNotificationChannel(channel);
            }
            Objects.requireNonNull(notificationManager).notify(0 /* ID of notification */, notification );
        }catch (Exception e){
            e.printStackTrace();
        }
    }

Спросите меня, что вы здесь не понимаете

...