Firebase 已收到Xamarin表单推送通知,但未显示/出现
我以前有推送通知,但由于某种原因,现在虽然收到了推送通知(我知道它收到了,因为它在调试中显示收到了FireBaseMessage),但它不再显示或没有显示通知 在这里可以看到接收到的消息是: 以下是MyFireBaseMessagingService.cs中的代码:Firebase 已收到Xamarin表单推送通知,但未显示/出现,firebase,xamarin,xamarin.forms,push-notification,firebase-cloud-messaging,Firebase,Xamarin,Xamarin.forms,Push Notification,Firebase Cloud Messaging,我以前有推送通知,但由于某种原因,现在虽然收到了推送通知(我知道它收到了,因为它在调试中显示收到了FireBaseMessage),但它不再显示或没有显示通知 在这里可以看到接收到的消息是: 以下是MyFireBaseMessagingService.cs中的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android; using Android.A
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;
namespace Chatfer.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
//These is how most messages will be received
System.Diagnostics.Debug.WriteLine(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
else
{
//Only used for debugging payloads sent from the Azure portal
SendNotification(message.Data.Values.First());
}
}
void SendNotification(string messageBody)
{
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("Chatfer Message")
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(messageBody)
.SetAutoCancel(false)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
}
发生什么事了?为什么通知突然停止出现?如有任何见解或建议,将不胜感激
编辑:将MyFirebaseMessagingService.cs更改为以下内容后,通知再次开始显示强>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;
namespace Chatfer.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
//These is how most messages will be received
System.Diagnostics.Debug.WriteLine(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
else
{
//Only used for debugging payloads sent from the Azure portal
SendNotification(message.Data.Values.First());
}
}
void SendNotification(string messageBody)
{
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("Chatfer Message")
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetChannelId("YourUniqueChannelID");
//var notificationManager = NotificationManager.FromContext(this);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel("YourUniqueChannelID", "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用安卓系统;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.Util;
使用系统诊断;
使用Firebase.Messaging;
名称空间Chatfer.Droid
{
[服务]
[IntentFilter(新[]{“com.google.firebase.MESSAGING_EVENT”}]
MyFirebaseMessagingService类:FirebaseMessagingService
{
const string TAG=“MyFirebaseMsgService”;
公共覆盖无效OnMessageReceived(RemoteMessage)
{
System.Diagnostics.Debug.WriteLine(标记“From:”+message.From);
if(message.GetNotification()!=null)
{
//这就是大多数信息的接收方式
System.Diagnostics.Debug.WriteLine(标记,“通知消息正文:”+Message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
其他的
{
//仅用于调试从Azure门户发送的有效负载
SendNotification(message.Data.Values.First());
}
}
void SendNotification(字符串messageBody)
{
var intent=新的intent(此,类型为(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent=pendingIntent.GetActivity(this,0,intent,PendingIntentFlags.OneShot);
var notificationBuilder=new Notification.Builder(此)
.SetContentTitle(“Chatfer消息”)
.SetSmallIcon(Resource.Drawable.ic_启动器)
.SetContentText(messageBody)
.SetAutoCancel(真)
.SetContentIntent(挂起内容)
.SetChannelId(“您的唯一channelid”);
//var notificationManager=notificationManager.FromContext(this);
if(Build.VERSION.SdkInt
我认为你没有得到它的原因是,如果你有Android V 8+设备,你没有通知通道,我相信你会注意到你正在注册MainActivity OnCreate方法中的通知通道
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
void CreateNotificationChannel()
{
if(Build.VERSION.SdkInt
你说得对,我没有这个,并且最近将我的应用程序的最低API版本更新为26。谢谢bhaiAt在OnCreate中,您需要调用CreateNotificationChannel方法吗?@Bedir我不认为有任何这样的限制,您只需在发送通知之前确保您有一个频道问题是我已将TargetAppVersion更新为26。请参阅?为了进一步了解情况,我希望我能多投十几票。你的编辑帮我解决了一个问题,我已经有好几天了。哈哈哈,没问题啊,祝你一切顺利!