我在Oreo设备上遇到问题,通知发送到客户端时,通知只是根本不显示在平视显示器上。我可以确保在背景中接收到它们,因为我创建了自定义振动效果OnMessageReceived,并且可以正常工作,但是在Android HUD中仍然没有显示任何内容。
此外,我注意到另一篇文章说,当强制关闭Visual Studio时,它会终止通知服务。的确如此,因为它也杀死了我的振动效果,但是在加载应用程序一面后,我可以从设备启动并触发我的自定义效果。
我相信我正在做的是1,将标签注册到Azure-FCM服务。 第二,我说的是注册我的API端点以及允许的模板。 我认为我注册的有效负载模板有问题, 以及我要发送的有效载荷
请参阅下文。
Web API that generates Android payload
private string GetAndroidAlertJson(int? fromId, int notificationType, string message)
{
return new JObject(
new JProperty("data",
new JObject(
new JProperty("notification",
new JObject(
new JProperty("badge", 1),
new JProperty("body", message),
new JProperty("title", "wazzup?!"),
new JProperty("priority", "high")
)
),
new JProperty("type_id", notificationType),
new JProperty("user", fromId != null ? fromId.ToString() : ""),
new JProperty("message", message)
)
)
).ToString();
}
Xamarin.Android
//NotificationBuilder
return new NotificationCompat.Builder(ApplicationContext, PRIMARY_CHANNEL)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(SmallIcon)
.SetPriority(2)
.SetVibrate(new long[0])
.SetAutoCancel(true);
//In MainActivity
public NotificationHelper(Context context) : base(context)
{
var chan1 = new NotificationChannel(PRIMARY_CHANNEL,
PRIMARY_CHANNEL, NotificationImportance.Max);
chan1.LightColor = 2;
chan1.LockscreenVisibility = NotificationVisibility.Public;
Manager.CreateNotificationChannel(chan1);
}
public NotificationCompat.Builder GetNotification1(String title, String body)
{
return new NotificationCompat.Builder(ApplicationContext, PRIMARY_CHANNEL)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(SmallIcon)
.SetPriority(2)
.SetVibrate(new long[0])
.SetAutoCancel(true);
}
FCMService
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMMessagingService : FirebaseMessagingService
{//Foreground Only
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
if (Debugger.IsAttached)
Debugger.Break();
CustomNotificationEffect();
try
{
if (message.Data.Count > 0)
{
var type_id = Convert.ToInt32(RetreiveNotification("type_id"));
var body = RetreiveNotification("message");
MainActivity.SendNotification(type_id, body);
}
}
catch (Exception e)
{
//Was NotificationType null? this should never be null anyways
}
string RetreiveNotification(string key)
{
string value = String.Empty;
message.Data.TryGetValue(key, out value);
return value;
}
}
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class FCMInstanceIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
public static string ProfilePushTag;
public async override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Console.WriteLine("Token Received!: " + refreshedToken);
await SendRegistrationToServer(refreshedToken);
}
public async Task SendRegistrationToServer(string token)
{
//We will have to send our token to the server here
//
string templateBodyFCM =
"{" +
"\"notification\" : {" +
"\"body\" : \"$(messageParam)\"," +
"\"title\" : \"Xamarin U\"," +
"\"icon\" : \"myicon\" }}";
var templates = new JObject();
//templateBodyFCM = "Pyx";
templates["genericMessage"] = new JObject
{
{ "body", templateBodyFCM}
};
try
{
var tags = new List<string>();
if (!String.IsNullOrWhiteSpace(ProfilePushTag))
{
// Register with Notification Hubs
var hub = new NotificationHub("HUB-NAME",
"AZURE-ENDPOINT"
, this);
tags.Add(ProfilePushTag);
var regID = hub.Register(token, tags.ToArray()).RegistrationId;
var hub2 = new MobileServiceClient("https://MyAPI");
await hub2.GetPush().RegisterAsync(token, templates);
}
}
catch (Exception e)
{
}
}
答案 0 :(得分:1)
在 Android O 中,必须在Notification Builder中使用频道 以下是应如何使用的示例代码:
public NotificationCompat.Builder GetNotification1(String title, String body) {
var mynotif = new Notification.Builder(ApplicationContext)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(SmallIcon)
.SetPriority(2)
.SetVibrate(new long[0])
.SetAutoCancel(true);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {
mynotif.SetChannelId(PRIMARY_CHANNEL);
}
}
答案 1 :(得分:0)
如果查看我的Web API,所有内容都包装在“数据”类型中。根据本文How to handle notification when app in background in Firebase,“数据”类型将不会显示在HUD中,但会显示“通知”类型。删除此属性可以解决此问题,并且现在显示在HUD中。