自定义通知托盘不适用于某些手机

Posted

技术标签:

【中文标题】自定义通知托盘不适用于某些手机【英文标题】:Custom Notification tray doesn't work for some phones 【发布时间】:2018-07-30 11:57:22 【问题描述】:

我正在开发一个将设备发送到设备推送通知的应用程序。我创建了一个自定义通知布局,它有一个标题、消息和两个按钮(接受和拒绝)。当设备 B 收到来自设备 A 的通知时,对于某些手机它可以正常工作,但在某些手机中,通知托盘无法显示标题、消息和按钮。它只显示一个空白通知托盘。这不是错误,但某些手机无法加载自定义布局。它在 Moto G5s plus (android 7.1.1) 中完美运行,但不适用于相同 API 级别 (Android 7.1.1) 的 RedMi Note 5 Pro。谁能帮我解决这个问题?

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
    Map<String, String> remoteMessageData = remoteMessage.getData();

    String remoteMessageType = remoteMessageData.get("type");


        String message = remoteMessageData.get("message") + ". Please Confirm?";
        String profilePhoto = remoteMessageData.get("profile_photo");
        String notificationUID = remoteMessageData.get("notification_uid");
        String userUID = remoteMessageData.get("user_uid");

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);

        remoteViews.setTextViewText(R.id.textNotificationMessage, message);

        remoteViews.setImageViewBitmap(R.id.eIntercomProfilePic, getBitmapFromURL(profilePhoto));

        Notification notification = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.drawable.namma_apartment_notification)
                .setAutoCancel(true)
                .setCustomBigContentView(remoteViews)
                .setSound(RingtoneManager.getDefaultUri(Notification.DEFAULT_SOUND))
                .setPriority(PRIORITY_DEFAULT)
                .build();

        int mNotificationID = (int) System.currentTimeMillis();

        Intent acceptButtonIntent = new Intent("accept_button_clicked");
        acceptButtonIntent.putExtra("Notification_Id", mNotificationID);
        acceptButtonIntent.putExtra("Notification_UID", notificationUID);
        acceptButtonIntent.putExtra("User_UID", userUID);
        PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(this, 123, acceptButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.buttonAccept, acceptPendingIntent);

        Intent rejectButtonIntent = new Intent("reject_button_clicked");
        rejectButtonIntent.putExtra("Notification_UID", notificationUID);
        rejectButtonIntent.putExtra("Notification_Id", mNotificationID);
        rejectButtonIntent.putExtra("User_UID", userUID);
        PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(this, 123, rejectButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.buttonReject, rejectPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    /*To support Android Oreo Devices and higher*/
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) 
            NotificationChannel mChannel = new NotificationChannel(
                    getString(R.string.default_notification_channel_id), "Namma Apartments Channel", NotificationManager.IMPORTANCE_HIGH);
            Objects.requireNonNull(notificationManager).createNotificationChannel(mChannel);
        

【问题讨论】:

【参考方案1】:

我认为您在谈论奥利奥或以上版本。 Oreo 不支持通知您需要为其创建频道的通知,如下所示:

 private void sendMyNotification(String message,String title) 
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) 
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            notificationManager.createNotificationChannel(notificationChannel);
        
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.listlogo)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.tlogo))
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));
            //.setProgress(100,50,false);
            notificationManager.notify(0, notificationBuilder.build());
    

【讨论】:

以上是关于自定义通知托盘不适用于某些手机的主要内容,如果未能解决你的问题,请参考以下文章

Android,自定义通知,总是给我白色文本

手机进入睡眠状态时不播放自定义通知声音

为啥 Galaxy S21 没有自定义通知声音

在 Android 中自定义通知区域

Android种使用Notification实现通知管理以及自定义通知栏(示例四)

在自定义通知布局中更新 TextView