在某些 Android 设备中收到空白通知

Posted

技术标签:

【中文标题】在某些 Android 设备中收到空白通知【英文标题】:Getting blank notification in some Android device 【发布时间】:2017-08-21 04:57:19 【问题描述】:

在某些设备中,我收到一个空白(白色)通知,如附加的屏幕截图。在某些设备中,它运行良好。请帮我解决这个问题。

 Intent intent = new Intent(ctx, NotificationDetailActivity.class);
            intent.putExtra("id", id);
            PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
            builder.setTicker(getResources().getString(R.string.app_name));
            // Sets the small icon for the ticker
            builder.setSmallIcon(getNotificationIcon());
            builder.setLargeIcon(result);
            builder.setColor(getResources().getColor(R.color.colorPrimary));
            builder.setContentTitle(title);
            builder.setContentText(messageBody);
            builder.setSound(defaultSoundUri);
            builder.setContentIntent(pendingIntent);
            builder.setAutoCancel(true);
            Notification notification = builder.build();
            RemoteViews expandedView =
                    new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
            if (Build.VERSION.SDK_INT >= 16)
            
                // Inflate and set the layout for the expanded notification view
                expandedView.setImageViewBitmap(R.id.imgBigImage, result);
                notification.bigContentView = expandedView;
                NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                nm.notify(Integer.parseInt(id), notification);
             else 
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                        .setSmallIcon(getNotificationIcon())
                        .setLargeIcon(result)
                        .setColor(getResources().getColor(R.color.colorPrimary))
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(Integer.parseInt(id), notificationBuilder.build());
            

【问题讨论】:

Lolipop 及以上版本您遇到了问题。不是吗? 在此处添加您的通知代码.. 请添加您的代码 @NilamVaddoriya 我添加了代码。请检查它 @AnkitaGuna 出现空白通知的那个设备的安卓版本是什么? 【参考方案1】:

试试这个方法setBigContentViewNotificationCompat.Builder ,检查它是否有效:-

Intent intent = new Intent(ctx, NotificationDetailActivity.class);
    intent.putExtra("id", id);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        Notification notification = builder.build();
        RemoteViews expandedView =
                new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
        if (Build.VERSION.SDK_INT >= 16)
        
            builder.setTicker(getResources().getString(R.string.app_name));
            // Sets the small icon for the ticker
            builder.setSmallIcon(getNotificationIcon());
            builder.setLargeIcon(result);
            builder.setColor(getResources().getColor(R.color.colorPrimary));
            builder.setContentTitle(title);
            builder.setContentText(messageBody);
            builder.setSound(defaultSoundUri);
            builder.setContentIntent(pendingIntent);
            builder.setAutoCancel(true);
            // Inflate and set the layout for the expanded notification view
            expandedView.setImageViewBitmap(R.id.imgBigImage, result);
//            notification.bigContentView = expandedView;
            builder.setCustomBigContentView(expandedView);
         else 
            builder.setSmallIcon(getNotificationIcon());
            builder.setLargeIcon(result);
            builder.setColor(getResources().getColor(R.color.colorPrimary));
            builder.setContentTitle(title);
            builder.setContentText(messageBody);
            builder.setAutoCancel(true);
            builder.setSound(defaultSoundUri);
            builder.setContentIntent(pendingIntent);
        
        nm.notify(Integer.parseInt(id), builder.build());

【讨论】:

@AnkitaGuna 现在问题可能与expandedView 有关。所以先评论builder.setCustomBigContentView(expandedView);这一行,试试看。 @AnkitaGuna 好的。然后找到并正确设置RemoteViews 将解决您的问题。 @NitinPatel 当我评论那行时它对我有用,但为什么呢?

以上是关于在某些 Android 设备中收到空白通知的主要内容,如果未能解决你的问题,请参考以下文章

某些设备在 android 中未收到推送通知

某些 android 设备未收到 GCM 推送通知

android推送通知显示空白内容

某些设备中的 Android 通知问题

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

如果 Android 设备长时间处于空闲状态,则不会收到推送通知