通知没有交付xamarin android



收到通知时,以下代码正在处理消息:

private void SendNotification(string message)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Notification.Builder(this)
.SetContentTitle("GCM Message")
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notificationBuilder.Build());
}

但什么都没有显示。我正在调试它,这会有什么不同吗?当我转到设备上应用程序的设置时,会选中"显示通知"。

评论1:

using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
{
var title = "Title";
var channelName = "TestChannel"
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel channel = null;
if (channel == null)
{
channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
{
LockscreenVisibility = NotificationVisibility.Public
};
channel.SetShowBadge(true);
notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();
}
var bitMap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.notification_template_icon_bg);
var notificationBuilder = new NotificationCompat.Builder(ApplicationContext)
.SetContentTitle(title)
.SetContentText(message)
.SetLargeIcon(bitMap)
.SetShowWhen(false)
.SetChannelId(channelName);
var notification = notificationBuilder.Build();
notificationManager.Notify(0, notification);
}

运行该代码,没有任何错误,但没有显示任何内容。

谢谢你的帮助!

随着更新的Android API,它们现在需要使用通知通道(NotificationChannel(。通过使用Android支持库中的NotificationCompat,只在使用奥利奥或更高版本时创建频道,您可以非常轻松地完成此操作。

NotificationCompat w/Channel示例:

using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
{
var channelName = GetText(Resource.String.notificationChannelNormal);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel channel = null;
#if !DEBUG
channel = notificationManager.GetNotificationChannel(channelName);
#endif
if (channel == null || resetChannel)
{
channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
{
LockscreenVisibility = NotificationVisibility.Public
};
channel.SetShowBadge(true);
notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();
}
Bitmap bitMap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
var notificationBuilder = new NotificationCompat.Builder(ApplicationContext)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.ic_stat_notification_network_locked)
.SetLargeIcon(bitMap)
.SetShowWhen(false)
.SetChannelId(channelName)
.SetContentIntent(pendingIntent);
return notificationBuilder.Build();
}

遵循这个指南帮助了我:

https://learn.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough

我想我没有看到任何通知的原因是我没有图标。在通知中添加图标后,一切正常。

结果:

[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
public class GcmNotificationService : GcmListenerService
{
//More information on how to set different things from notification can be found here
//https://learn.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications
public override void OnMessageReceived(string from, Bundle data)
{
var message = data.GetString("message");
if (!string.IsNullOrEmpty(message))
{
if (!NotificationContextHelper.Handle(message))
SendNotification(message);
}
}
private void SendNotification(string message)
{
var builder = new Notification.Builder(this)
.SetContentTitle("Title")
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.notification_test)
.SetVisibility(NotificationVisibility.Public);
var notification = builder.Build();
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
notificationManager.Notify(0, notification);
}
}

需要注意的是,在AndroidManifest.xml中使用了完全限定的类名,例如:android:name="com.companyname.myapp.MyFirebaseMessagingService";

AND实现类的服务的命名空间应该是com.companyname.myapp.

相关内容

  • 没有找到相关文章

最新更新