简体   繁体   English

Android:图标更改时未发送和显示通知

[英]Android : Notification not sent & shown when Icon is changed

I am working on an Android project in which I am sending out notifications whenever there is an event happening. 我正在开发一个Android项目,无论何时发生事件,我都会在其中发送通知。 Unfortunately, when I change the icon of Notification to our project icon, which is a 8.4kb image, I don't get any notification. 不幸的是,当我将“通知”图标更改为我们的项目图标(这是一个8.4kb的图像)时,没有收到任何通知。 This is especially problematic as there is no error thrown, just no notificiations are received. 这是特别有问题的,因为不会引发任何错误,只会收到任何通知。

When I change the image to a simple red-square, I can see the notification, but the notification is not even red colored. 当我将图像更改为简单的红色正方形时,可以看到通知,但是通知甚至不是红色。 How can I properly set the Notification image to desired image. 如何将通知图像正确设置为所需图像。 Thank you. 谢谢。

As you can see the first notification, the icon is not proper. 如您所见,第一个通知是图标不正确。

Screenshot : 屏幕截图:

在此处输入图片说明

Code : 代码:

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setAutoCancel(true);

mBuilder.setSmallIcon(R.drawable.defaultimage);
mBuilder.setContentTitle(subject);
mBuilder.setContentText(Html.fromHtml(text));

 if (type.equals("note")) {
                    Log.d("type","note");
                    Intent resultIntent = new Intent(getApplication(), EditNoteActivity.class);
                    resultIntent.putExtra("groupid", Long.valueOf(channelName));
                    resultIntent.putExtra("canvasid", Integer.valueOf(canvasId));
                    resultIntent.putExtra("sectionid", Integer.valueOf(sectionId));
                    resultIntent.putExtra("noteid", Integer.valueOf(noteId));

                    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
                    stackBuilder.addParentStack(EditNoteActivity.class);

                    stackBuilder.addNextIntent(resultIntent);
                    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                    mBuilder.setContentIntent(resultPendingIntent);

                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                    mNotificationManager.notify(notificationCounter, mBuilder.build());
                    notificationCounter++;
}

The image which I am trying to set is a PNG image, of 8kb, doesn't help setting it any way. 我尝试设置的图像是8kb的PNG图像,无济于事。 Any help would be nice. 你能帮忙的话,我会很高兴。 Thank you. 谢谢。

Update 更新资料

When I select the image, The ide shows the image properly as seen from the screenshot : 当我选择图像时,如屏幕截图所示,ide将正确显示图像: 在此处输入图片说明

Even if the IDE shows it correctly, the image received in notification is not correct. 即使IDE正确显示它,通知中收到的图像也不正确。

Now, when I try to add it as an asset, it shows preview very wrong. 现在,当我尝试将其添加为资产时,它显示预览非常错误。 And the images generated are also wrong. 并且生成的图像也是错误的。

Screenshot : 屏幕截图:

在此处输入图片说明

As you can see, it just says the image is some gray color, but its a blue colored image. 如您所见,它只是说图像是灰色,但是是蓝色图像。

Aloks suggestion 阿罗克斯的建议

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
                mBuilder.setAutoCancel(true);
                mBuilder.setSmallIcon(R.mipmap.twentynotelogo);
                Bitmap icon = BitmapFactory.decodeResource(getResources(),
                        R.mipmap.twentynotelogo);
                mBuilder.setLargeIcon(icon);
                mBuilder.setContentTitle(subject);
                mBuilder.setContentText(Html.fromHtml(text));

You need to implement new specification for notification, Need to use setLargeIcon(),together with transparent small icon. 您需要实现新的通知规范,需要使用setLargeIcon()以及透明的小图标。

in your case you are using setSmallIcon() only that is causing issue. 在您的情况下,您仅使用setSmallIcon()会导致问题。 Just set both small and large icon it will work.( it is an update in lollipop) 只需设置大小图标即可使用(这是棒棒糖的更新)

update: if you only need to set small icon than you need to use bitmap and apply some background color 更新:如果只需要设置小图标,则不需要使用位图并应用一些背景色

Try to get the image as a drawable from the Android Studio as a Notifications Icon. 尝试从Android Studio中将其作为可绘制的图像获取为通知图标。 Also use setLarge() Icon as well. 也可以使用setLarge()图标。 While selecting the resource select it is a Image asset as shown below. 选择资源时,选择它是图像资产,如下所示。 通知图标生成Android Studio

The notification icon shows in preview what it will look like based on the API levels. 通知图标会预览显示基于API级别的外观。

For your icon you need to use it as a Transparent Icon and then import in the above method itself. 对于您的图标,您需要将其用作透明图标,然后以上述方法本身导入。 Remove the blue layer from the icon then load it in the Android Studio. 从图标中删除蓝色层,然后将其加载到Android Studio中。 You will get this as shown below. 您将如下所示。 Android Studio中的透明图标

Then if you still want blue color in your icon you need to add background color programmatically to your NotificationCompat.Builder as 然后,如果您仍想在图标中使用蓝色,则需要以编程方式将背景色添加到NotificationCompat.Builder中,如下所示:

int notificationcolor = getResources().getColor(R.color.my_notif_color);

mBuilder.setColor(notificationcolor);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM