简体   繁体   English

适用于iOS的Firebase FCM静默推送通知

[英]Firebase FCM silent push notifications for iOS

I have a problem with silent notifications on iOS. 我在iOS上的静音通知方面遇到问题。

When my application is in background, I don't receive silent notification sent by FCM. 当我的应用程序在后台运行时,我不会收到FCM发送的静默通知。 But if I try to send directly to APNS, the notification is successfully received. 但是,如果我尝试直接发送到APNS,则会成功收到通知。

This is the JSON sent to FCM: 这是发送到FCM的JSON:

{ 
"to" : "<token>",
"priority": "high",
"content_available": true,
"data" : {
  "<key>" : "<string>",
  "<key2>" : "<string>"
}

} }

This is the JSON sent directly to APNS: 这是直接发送到APNS的JSON:

{
  "aps": {
    "content-available": 1
  },
  "<key>": "<string>",
  "<key>": "<string>"
}

I have already tried to remove the "priority" key because I saw someone saying that I shouldn't set the priority if the "content_available" is already set. 我已经尝试删除“优先级”键,因为我看到有人说如果已经设置了“ content_available”,则不应该设置优先级。 It didn't work. 没用

  1. I have "Push Notifications" enabled in XCode > Capabilities. 我在XCode>功能中启用了“推送通知”。
  2. I have "Remote notifications" checked in Background Modes in XCode > Capabilities. 我在“ XCode”>“功能”的“后台模式”中选中了“远程通知”。
  3. The FCM notifications are working fine when app is in foreground and sometimes when the app is in background. 当应用程序在前台时,有时在应用程序在后台时,FCM通知工作正常。

Remove "notification" key value pair and add "content_available": true 删除“通知”键值对并添加“ content_available”:true

It will look like this 看起来像这样

{ 
    "to" : "...",
    "priority": "high",
    "content_available": true,
    "data" : {
      ....
    }
}

This should make it a silent APNS and you need to handle with corresponding APNS delegate method. 这应该使其成为一个静默的APNS,您需要使用相应的APNS委托方法进行处理。

You will need to handle this through delegates Refer this firebase documentation for details: https://firebase.google.com/docs/cloud-messaging/concept-options 您将需要通过代理进行处理。有关详细信息,请参阅此Firebase文档: https : //firebase.google.com/docs/cloud-messaging/concept-options

I found an workaround. 我找到了解决方法。 I put an empty value for "sound" in "notification" field and the silent notifications are delivered even when the application is in background. 我在“通知”字段中为“声音”输入了一个空值,即使应用程序处于后台,也会传递无提示通知。

{ 
    "to" : "...",
    "priority": "high",
    "notification": {
        "sound": ""
    },
    "data" : {
      ....
    }
}

My hunch is that Apple does not allow silent notifications with a 'high' priority and somehow "notification": {"sound": ""} tricks the APNS that this notification is not a silent one. 我的直觉是,Apple不允许具有“高”优先级的无提示通知,并且以某种方式“ notification”:{“ sound”:“”}}欺骗了APNS该通知不是无提示通知。

I was working on Firebase silent push notification using nodejs. 我正在使用nodejs进行Firebase静默推送通知。 When I tried below code its was working fine. 当我尝试下面的代码时,它工作正常。 When I was adding "priority": "high" and "content_available": true it was giving below error. 当我添加“ priority”:“ high”和“ content_available”:true时,它给出了以下错误。

Worked below code 工作于以下代码

const admin = require('firebase-admin');
const serviceAccount ="...."; //service account path
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

let  fcmToken = "...."; // Your token
let message ={
    "token": fcmToken,
    "data": {
        "updateApi": "activity"
    }
} 

admin.messaging().send(message)
  .then((response) =>{
    console.log('Successfully sent notification:', response);
})
  .catch((error) =>{
    console.log('Error while sending notification:', error);
});

Error when I added the priority and content_available in message object 在消息对象中添加优先级content_available时出错

{ code: 'messaging/invalid-argument',
     message: 'Invalid JSON payload received. Unknown name "priority" at \'message\': Cannot find field.\nInvalid JSON payload received. Unknown name "content_available" at \'message\': Cannot find field.' },
  codePrefix: 'messaging' }

Please follow the documentation for server side and make setup for json as explained over the document. 请遵循服务器端文档,并按照文档中的说明进行json的设置。 I have faced similiar problem earlier and solved the issue going this doc. 我之前曾遇到过类似的问题,并通过本文档解决了该问题。

    {
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "priority" : "high",
  "content_available": true,
  "notification" : {
    "body" : "",
    "title" : "",
    "icon" : "new"
  },
  "data" : {
    "volume" : "3.21.15",
    "contents" : "http://www.news-magazine.com/world-week/21659772"
  }
}

content_available = true and body, title empty does the task. content_available = true和body,标题为空则执行任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM