我在奥利奥设备上遇到了一个问题,通知在发送到客户端时根本不显示在 hud 中。我可以确保它们在后台被接收,因为我已经创建了一个自定义振动效果 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)
{
}
}
在Android O中,必须在通知生成器中使用频道 下面是您应该如何使用它的示例代码:
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);
}
}
如果你看看我的 Web API,一切都包装在一个"数据"类型中。根据这篇文章如何处理通知,当应用程序在Firebase的后台运行时,"数据"类型不会显示在HUD中,但"通知"类型会。删除此属性解决了问题,现在显示在 HUD 中。