FCM推送通知不会在屏幕顶部显示通知头

时间:2017-04-11 14:58:07

标签: android firebase firebase-cloud-messaging

我们在Android和iOS中使用firebase推送通知。使用FCM REST API调用发送推送。推送类型为notification,带有额外的data节点。

以下是有效负载示例:

{ 
  "notification" : {
    "title": "title text",
    "body": "message body text",
    "sound": "default"
  },
  "data":  {
    "messageType": "xxx"
  },
  "to": "yyy",
  "priority": "high",
  "time_to_live": 0
}

当应用程序处于后台并且手机处于打开状态时,此类推送通知不显示抬头显示。 - 通知只会添加到通知栏中,但不会在屏幕顶部潜入峰值。 - 无论当前的应用程序是否是全屏应用程序。

我尝试过并且正在工作的一个解决方案是转移到纯data消息,我们不会发送任何notification节点,而只发送data节点并将代码写入自己显示通知并在通知构建器对象上将通知优先级设置为Max(即.setPriority(Notification.PRIORITY_MAX))。 但是这似乎在iOS上存在问题,如果该应用程序被用户杀死,则只会向用户显示data仅推送private static void AddToXmlTemplate(Template tmp, string _config) { string configFile = _config + "configuredTemplate.xml"; if (!File.Exists(configFile)) { FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate) XDocument xD = new XDocument(); xD.Add(new XElement("Store", new XElement("template", new XElement("filePath", tmp.TempPath), new XElement("Name", tmp.TempName), new XElement("description", tmp.TempDesc)))); xD.Save(fs); fs.Flush(); fs.Dispose(); //commenting for change to allow sync. } else { FileStream fs = new FileStream(configFile, FileMode.Open); /********------ appends the template to the config file.------*************/ XDocument xD = XDocument.Load(fs); XElement root = xD.Element("Store"); IEnumerable<XElement> rows = root.Descendants("template"); XElement last = rows.Last(); last.AddAfterSelf( new XElement("template"), new XElement("filePath", tmp.TempPath), new XElement("Name", tmp.TempName), new XElement("description", tmp.TempDesc)); xD.Save(fs); fs.Flush(); fs.Dispose(); } }

那么有什么解决方法吗?任何适用于Android的解决方案,但也不会破坏iOS。

1 个答案:

答案 0 :(得分:0)

您应将Android的NotificationPriority设置为PRIORITY_HIGH或PRIORITY_MAX,以显示通知。有关更多详细信息,请参见Android NotificationNotification Priority

您应该使用常量而不是字符串来设置这些值。例如

{
  token,
  android: {
    notification:
    {
      title,
      body,
      // See https://developer.android.com/reference/android/app/Notification#PRIORITY_MAX.
      // This is deprecated in Android API level 26, but works for older versions.
      notification_priority: 2,
      // Always allow Android users to see the message in its entirety.
      // See https://developer.android.com/reference/android/app/Notification#VISIBILITY_PUBLIC
      visibility: 1
    }
  }
}