在.NET MAUI Android中的通知图标

huangapple go评论68阅读模式
英文:

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:

Inside the app:
在.NET MAUI Android中的通知图标

And if I scroll down while in the app:
在.NET MAUI Android中的通知图标

Outside the app (when closed):
在.NET MAUI Android中的通知图标

And again, if I scroll down while the app is closed:
在.NET MAUI Android中的通知图标

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:

Inside the app:
在.NET MAUI Android中的通知图标

And if I scroll down while in the app:
在.NET MAUI Android中的通知图标

Outside the app (when closed):
在.NET MAUI Android中的通知图标

And again, if I scroll down while the app is closed:
在.NET MAUI Android中的通知图标

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

简单解决方案:

  1. 替换资源中的图像:Resources > Images > dotnet_bot.svg
  2. 现在,使用smalliconref dotnet_bot:FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot
  3. 示例:
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:

  1. replace the image in Resources > Images > dotnet_bot.svg

  2. Now, use the smalliconref dotnet_bot FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.dotnet_bot

  3. 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>

huangapple
  • 本文由 发表于 2023年2月14日 07:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定