通知未传送Xamarin Android [英] Notifications are not delivering xamarin android

查看:60
本文介绍了通知未传送Xamarin Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收到通知后,以下代码将处理该消息:

When the notification is recieved following code is handling the message:

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());
}

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

But nothing shows. I'm debugging it it that would make any difference? When I go to settings on the app on the device, "Show notifications" is checked.

评论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);
}

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

Runnig that code, no errors but nothing shows up.

感谢您的帮助!

推荐答案

遵循该Guid帮助我:

Following this guid helped me:

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

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

I think the reason I didn't see any notifications was that I didn't have an icon. After adding an icon to the notification, everything worked.

结果:

[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://docs.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);
    }
}

这篇关于通知未传送Xamarin Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆