代码之家  ›  专栏  ›  技术社区  ›  Devrath

推送通知图标在Android的棉花糖设备上显示得非常小

  •  0
  • Devrath  · 技术社区  · 7 年前

    我正在使用代码:

    /** Prepare the notification UI **/
        private void prepareNotificationUI(String mTitle, String mDescription, int notificationId, Bundle bundle) {
    
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.setAction(Long.toString(System.currentTimeMillis()));
            intent.putExtras(bundle);
    
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(intent);
    
            PendingIntent pendingIntent = stackBuilder
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                            | PendingIntent.FLAG_ONE_SHOT);
    
    
    
            NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());
                nb = notificationHelper.getNotification(pendingIntent, mTitle, mDescription);
                if (nb != null) {
                    notificationHelper.notify(notificationId, nb);
                }
            }else {
    
                Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                String channelId = mContext.getString(R.string.app_name);
    
                Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.notification_icon);
    
    
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, channelId);
                //Notification.Builder notificationBuilder=new Notification.Builder(mContext);
    
                notificationBuilder.setContentTitle(mTitle);
                notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mDescription));
                notificationBuilder.setContentText(mDescription);
                notificationBuilder.setAutoCancel(true);
                notificationBuilder.setSound(defaultSoundUri);
    
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    notificationBuilder.setSmallIcon(R.drawable.notification_icon);
                    notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));
                    notificationBuilder.setLargeIcon(largeIcon);
                } else {
                    notificationBuilder.setSmallIcon(R.drawable.notification_icon);
                    notificationBuilder.setLargeIcon(largeIcon);
                }
    
                /*notificationBuilder.setSmallIcon(R.drawable.notification_icon);
                notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));*/
                notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
                notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
                notificationBuilder.setLights(Color.WHITE, 3000, 3000);
                notificationBuilder.setContentIntent(pendingIntent);
    
                notificationManager.notify(notificationId, notificationBuilder.build());
    
            }
    
        }
    

    • 我在Marshmellow设备中得到的图标非常小。很好用
    • 早些时候,这个图标根本没有在Marshmellow中显示出来。然后我 添加了下面的代码。图标正在显示,但大小很小

      notificationBuilder.setLargeIcon文件(largeIcon公司);

    2 回复  |  直到 7 年前
        1
  •  0
  •   InsaneCat    7 年前

    试着这样做:

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            } else {
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            }
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(id, notificationBuilder.build());
    

    希望这对你有帮助

        2
  •  0
  •   Paras Verma    7 年前

    Bitmap bigIcon = BitmapFactory.decodeResource(mContext.getResources(),
                R.mipmap.ic_launcher);
    
    推荐文章