通知在某些设备上不显示

时间:2019-06-16 15:30:40

标签: android android-notifications

我正在尝试在其他设备上测试我的推送通知功能。现在,当我将Samsung Galaxy S9 +与android Pie一起使用时,该通知将按应有的方式显示-我看到标题,看到机身,看到图标。在带有Android Pie的Galaxy S8上进行测试时,我既看不到标题也没有看到正文,仅显示图标。 在Android Oreo的Pixel 2XL上进行测试时,其行为与Galaxy S9 +相同,但是在Android的Pixel 2XL上进行测试时,其行为与Galaxy S8相同。是什么导致行为在不同设备上发生变化?我该如何解决?

这是我用来显示

的代码
private void showNotifications(String title, String msg, String deepLink){
        msg = msg.replace("<br/>", "\n");
        Spanned htmlText = Html.fromHtml(msg);
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
        contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher_foreground);
        contentView.setTextViewText(R.id.title, title);
        contentView.setTextViewText(R.id.text, htmlText);

        Intent intent ;

        if (deepLink!=null)
            intent = new Intent(deepLink);
        else
            intent = new Intent(this, MainActivity.class);

        //get desired location from deep link
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra("title", title);
        intent.putExtra("message", msg);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        Random rand = new Random();
        int  notificationID = rand.nextInt(50000);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

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

            NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), NOTIF_CHANNEL_ID)
                    .setContent(contentView)
                    .setContentIntent(pendingIntent)
                    .setSound(defaultSoundUri)
                    .setSmallIcon(R.drawable.status_bar_icon)
                    .setAutoCancel(true)
                    .setChannelId(NOTIF_CHANNEL_ID);

            NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID, NOTIF_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setShowBadge(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            channel.enableVibration(true);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
//            manager.notify(notificationID, notification.build());
            manager.notify(NOTIF_CHANNEL_ID, notificationID, notification.build());

        }
        else
        {
            Notification notification = new NotificationCompat.Builder(this)
                    .setContent(contentView)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.status_bar_icon)
                    .setSound(defaultSoundUri)
                    .setAutoCancel(true)
                    .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                    .build();

            NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            manager.notify(notificationID, notification);
        }
    }

这就是它的外观(这是在Android Pie上的外观-Galaxy S9 +和配备Android Oreo的Pixel 2XL) enter image description here

这是使用Android Pie的Pixel 2XL和带有Android Pie的Galaxy S8的外观。 (通知中缺少该文本) enter image description here

0 个答案:

没有答案
相关问题