Android API 29 上未显示通知

Android notification not shown on API 29

我在 Android API 29 上显示通知时遇到问题。虽然代码基于 this 教程,但执行代码时没有显示通知。

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)
        }
    }

}

这是我从第 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();
        }
    }

有不懂的地方问我

正如 Mike M. 指出的那样: "It looks like you might be using two different channel IDs; NOTIFICATION_GROUP_LOCATION and NOTIFICATION_CHANNEL_LOCATION. The ID you pass in NotificationCompat.Builder(this, ...) needs to be the same as the one passed in NotificationChannel(..., "群组位置", IMPORTANCE_DEFAULT)."

更改 ID 后显示通知。