英文:
Notification Icon in .NET MAUI Android
问题
I'm facing an issue with push notifications in .NET MAUI. I'm using Firebase Cloud Messaging and I have successfully integrated it into my app. The problem is with the notification icon; when I receive a notification while using the app, the icon is displayed correctly, but if I receive the notification when the app is closed, the icon appears small and inside a larger circle (which doesn't look good). I've tried using both SVG and PNG files with colored or white and transparent backgrounds, but nothing seems to work. That's the image I have used.
Here are the screenshots of the problem:
And if I scroll down while in the app:
Outside the app (when closed):
And again, if I scroll down while the app is closed:
These examples were made using a PNG file named small_notification_icon.png and saved in the Resources/Images folder, which only uses white color with a transparent background. This is the code I used:
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.ChannelID)
.SetSmallIcon(Resource.Drawable.small_notification_icon)
.SetContentTitle(title)
.SetContentText(messageBody)
.SetChannelId(MainActivity.ChannelID)
.SetPriority((int)NotificationPriority.High);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NotificationID, notificationBuilder.Build());
I would appreciate any help or suggestions on how to resolve this issue.
英文:
I'm facing an issue with push notifications in .NET MAUI. I'm using Firebase Cloud Messaging and I have successfully integrated it into my app. The problem is with the notification icon; when I receive a notification while using the app, the icon is displayed correctly, but if I receive the notification when the app is closed, the icon appears small and inside a larger circle (which doesn't look good). I've tried using both SVG and PNG files with colored or white and transparent backgrounds, but nothing seems to work. That's the image I have used.
Here are the screenshots of the problem:
And if I scroll down while in the app:
Outside the app (when closed):
And again, if I scroll down while the app is closed:
These examples were made using a PNG file named small_notification_icon.png and saved in the Resources/Images folder, which only uses white color with a transparent background. This is the code I used:
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.ChannelID)
.SetSmallIcon(Resource.Drawable.small_notification_icon)
.SetContentTitle(title)
.SetContentText(messageBody)
.SetChannelId(MainActivity.ChannelID)
.SetPriority((int)NotificationPriority.High);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NotificationID, notificationBuilder.Build());
I would appreciate any help or suggestions on how to resolve this issue.
答案1
得分: 1
I've identified the issue. When the app runs in the background, notifications sent from the console use the launcher icon designated in the manifest file.
To address the problem, you can simply replace the default image specified in the AndroidManifest
by inserting the following code within the Application
tag. All you need to do is substitute your_favourite_image with the image you prefer for the notification icon.
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/your_favourite_image" />
This should resolve the issue and allow you to use your preferred image for notifications.
英文:
I've identified the issue. When the app runs in the background, notifications sent from the console use the launcher icon designated in the manifest file.
To address the problem, you can simply replace the default image specified in the AndroidManifest
by inserting the following code within the Application
tag. All you need to do is substitute your_favourite_image with the image you prefer for the notification icon.
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/your_favourite_image" />
This should resolve the issue and allow you to use your preferred image for notifications.
答案2
得分: 0
我使用了.SetSmallIcon(Resource.Drawable.appiconfg)
,它很好。它使用了我的应用图标,这个图标是项目中的一个SVG文件。
英文:
I used .SetSmallIcon(Resource.Drawable.appiconfg)
and it is fine. It uses my app icon, which is an SVG file in the project.
答案3
得分: 0
简单解决方案:
- 替换资源中的图像:Resources > Images > dotnet_bot.svg
- 现在,使用smalliconref dotnet_bot:
FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot
- 示例:
private void CreateNotificationChannel()
{
var channelId = $"{PackageName}.general";
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
var channel = new NotificationChannel(channelId, "General", NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
FirebaseCloudMessagingImplementation.ChannelId = channelId;
FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot;
}
英文:
Easy resolution:
-
replace the image in Resources > Images > dotnet_bot.svg
-
Now, use the smalliconref dotnet_bot
FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot
-
Example:
<pre>
<code>private void CreateNotificationChannel()
{
var channelId = $"{PackageName}.general";
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
var channel = new NotificationChannel(channelId, "General", NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
FirebaseCloudMessagingImplementation.ChannelId = channelId;
FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot;
}
</code>
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论