当应用程序关闭/后台运行时,Android FCM通知不会显示在HUD中
我在Oreo设备上遇到了一个问题,当通知发送到客户端时,通知只是不显示在hud中。我可以保证它们是在后台接收的,因为我已经在MessageReceived上创建了一个自定义的振动效果,这很有效,但Android HUD中仍然没有显示任何内容 另外,我还注意到另一篇文章说VisualStudio在强制关闭通知服务时会终止通知服务。这是真的,因为它也消除了我的振动效果,但在应用程序端加载后,我可以从设备启动,并触发我的自定义效果 我相信我正在做的是1,将我的标签注册到Azure FCM服务。 第二,我是说注册我的API端点和允许的模板。 我想我注册的有效负载模板有问题, 还有我发送的有效载荷 见下文当应用程序关闭/后台运行时,Android FCM通知不会显示在HUD中,android,firebase,xamarin.android,firebase-cloud-messaging,Android,Firebase,Xamarin.android,Firebase Cloud Messaging,我在Oreo设备上遇到了一个问题,当通知发送到客户端时,通知只是不显示在hud中。我可以保证它们是在后台接收的,因为我已经在MessageReceived上创建了一个自定义的振动效果,这很有效,但Android HUD中仍然没有显示任何内容 另外,我还注意到另一篇文章说VisualStudio在强制关闭通知服务时会终止通知服务。这是真的,因为它也消除了我的振动效果,但在应用程序端加载后,我可以从设备启动,并触发我的自定义效果 我相信我正在做的是1,将我的标签注册到Azure FCM服务。 第二,
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();
}
泽马林,安卓
//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)
{
}
}
//NotificationBuilder
返回新的NotificationCompat.Builder(ApplicationContext,主通道)
.SetContentTitle(标题)
.SetContentText(正文)
.SetSmallIcon(SmallIcon)
.SetPriority(2)
.SETVIBRIVE(新长[0])
.SetAutoCancel(真);
//主要活动
公共NotificationHelper(上下文):基(上下文)
{
变量通道1=新通知通道(主通道,
主信道,通知重要性.Max);
chan1.LightColor=2;
chan1.LockscreenVisibility=NotificationVisibility.Public;
创建通知频道(通道1);
}
public NotificationCompat.Builder GetNotification1(字符串标题、字符串正文)
{
返回新的NotificationCompat.Builder(ApplicationContext,主通道)
.SetContentTitle(标题)
.SetContentText(正文)
.SetSmallIcon(SmallIcon)
.SetPriority(2)
.SETVIBRIVE(新长[0])
.SetAutoCancel(真);
}
FCM服务
[服务]
[IntentFilter(新[]{“com.google.firebase.MESSAGING_EVENT”}]
公共类FCM消息服务:FirebaseMessagingService
{//仅前景
const string TAG=“MyFirebaseMsgService”;
公共覆盖无效OnMessageReceived(RemoteMessage)
{
if(Debugger.IsAttached)
Debugger.Break();
CustomNotificationEffect();
尝试
{
如果(message.Data.Count>0)
{
var type_id=Convert.ToInt32(RetreiveNotification(“type_id”));
var body=重新发布通知(“消息”);
MainActivity.SendNotification(类型\ id,正文);
}
}
捕获(例外e)
{
//NotificationType是否为null?无论如何,它都不应为null
}
字符串重新识别(字符串键)
{
字符串值=string.Empty;
message.Data.TryGetValue(键,输出值);
返回值;
}
}
[服务]
[IntentFilter(新[]{“com.google.firebase.INSTANCE\u ID\u EVENT”}]
公共类FCMInstanceIDService:firebaseInstancedService
{
const string TAG=“MyFirebaseIIDService”;
公共静态字符串ProfilePushTag;
公共异步重写无效OnTokenRefresh()
{
var refreshedToken=FirebaseInstanceId.Instance.Token;
Console.WriteLine(“收到令牌:”+refreshedToken);
等待发送注册到服务器(刷新完毕);
}
公共异步任务SendRegistrationToServer(字符串令牌)
{
//我们必须将令牌发送到这里的服务器
//
字符串模板BodyFCM=
"{" +
“\”通知\“:{”+
“\”正文\“:\”$(messageParam)\”+
“\“标题\:\“Xamarin U\”,”+
“\”图标\“:\”我的图标\“}}”;
var templates=newjobject();
//templateBodyFCM=“Pyx”;
模板[“genericMessage”]=新作业对象
{
{“body”,templateBodyFCM}
};
尝试
{
var tags=新列表();
如果(!String.IsNullOrWhiteSpace(ProfilePushTag))
{
//向通知中心注册
var hub=新通知中心(“中心名称”,
“AZURE-ENDPOINT”
,这个);
添加(ProfilePushTag);
var regID=hub.Register(token,tags.ToArray()).RegistrationId;
var hub2=新的MobileServiceClient(“https://MyAPI");
wait hub2.GetPush().RegisterAsync(令牌、模板);
}
}
捕获(例外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,一切都被包装在一个“数据”类型中。根据本文,“数据”类型将不会显示在HUD中,而是显示在“通知”中类型将。删除此属性解决了问题,现在显示在HUD中。在FCM中,客户端注册接收到的可接受的有效负载模板。可能有一个线索是我正在使用的模板与服务器一起注册,该模板为string templateBodyFCM=“{“+”\”notification\:{“+”\”正文\“:\”$(messageParam)\”,“+”\“标题\“:“Xamarin U\”,“+”\“图标\:“myicon\”}”;我刚刚读到键“通知”在后台不起作用您是否为oreo创建了通知通道?如果没有,请检查是的,我创建了通知通道并使用NotificationManager.CreateFromNotificationChannel方法…我想答案可能在这里是的,那么您为什么不更改有效负载呢?