通知未传送Xamarin Android

时间:2018-07-09 21:26:04

标签: c# push-notification xamarin.android firebase-cloud-messaging

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

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

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

使用更新的Android API,它们现在需要使用通知通道(NotificationChannel)。您可以使用Android支持库中的NotificationCompat轻松地完成操作,并且仅在Oreo或更高版本上才创建频道。

带频道示例的NotificationCompat:

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

答案 1 :(得分:1)

遵循这个指南对我有帮助:

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