我们目前正在评估Firebase作为未来的推送通知服务。 有没有办法向iOS设备发送可操作的通知? 目前我们使用parse发送推送,我们设置"类别"有效负载中的参数以及通知上的其他操作正在运行。 我们尝试在firebase控制台中或通过firebase rest api设置此参数,但通知操作无法正常工作,似乎有效负载与iOS预期有所不同。
答案 0 :(得分:16)
感谢Malik的回答。 FCM似乎将特定于Android的“click_action”属性转换为iOS特定的“category”属性。
我们通过他们的REST API发送firebase推送通知,这可以很容易地用于邮递员的测试。
以下是REST版本:
发布 https://fcm.googleapis.com/fcm/send
接头:
体:
{ "notification": {
"text": "YOUR_PUSH_TEXT",
"click_action":"YOUR_IOS_ACTIONABLE_NOTIFICATION_CATEGORY"
},
"to" : "YOUR_PUSH_TOKEN",
"data": {
"YOUR_CUSTOM_DATA": "DATA"
}
}
答案 1 :(得分:9)
目前 FCM控制台不支持类别,但如果您想测试,则可以使用curl post call和test。您可以从服务器向您的有效负载添加类别,并使用FCM api将通知推送到iOS。
curl --header "Authorization: key=<YOUR_SERVER_KEY>" --header Content- Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"Device Token\",\"priority\":\"high\",\"notification\": {\"title\": \"Shift Alert\",\"text\": \"Would you like to accept shift today 11:30 to 13:30 \",\"click_action\":\"INVITE_CATEGORY\"}}"
授权:key = YOUR_SERVER_KEY 确保这是服务器密钥,其值在Project Settings&gt;下的Firebase项目控制台中可用。云消息传递。 FCM拒绝Android,iOS和浏览器密钥。
INVITE_CATEGORY =您在代码中使用的类别
下面是您将点击操作的响应词典:
{
aps = {
alert = {
body = "Would you like to accept shift today 11:30 to 13:30 ";
title = "Shift Alert";
};
category = "INVITE_CATEGORY";
};
"gcm.message_id" = "0:12233487r927r923r7329";
}
答案 2 :(得分:0)
我为本地PC创建了一个简单的JS方法来发送带有类别的推送通知。我正在使用Node。
function sendFirebaseNotification(title, body, postId, postUrl, topic) {
var fbAdmin = require('firebase-admin');
var serviceAccount = require('./your_credential_file.json'); //download from firebase admin page and specify in your local machine
var app = fbAdmin.initializeApp({
credential: fbAdmin.credential.cert(serviceAccount)
});
var message = {
apns: {
payload: {
aps: {
alert: {
title: title,
body: body
},
category: "VIEW_NOTIFICATION" //your ios category
},
id: postId, //extra data
title: title, //extra data
url: postUrl //extra data
}
},
topic: topic //your ios app should subscribe to this topic (or you can use a specific token here).
};
// Send above message
fbAdmin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
process.exit();
})
.catch((error) => {
console.log('Error sending message:', error);
process.exit();
});
}
简单通话
sendFirebaseNotification(title, description, id, url, topic);
IOS句柄:
//Call when application loaded
func registerNotification(_ application: UIApplication) {
//Firebase callback
Messaging.messaging().delegate = self
Messaging.messaging().subscribe(toTopic: YOUR_TOPIC_NAME) { error in
print("Subscribed to notification topic")
}
//IOS Notification (ios 10 and above)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .badge, .sound],
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
//Add custom actions
let acceptAction = UNNotificationAction(identifier: "view_now",
title: "Xem ngay",
options: .foreground)
let skipAction = UNNotificationAction(identifier: "skip",
title: "Bỏ qua",
options: .foreground)
// Define the notification type
let viewCategory =
UNNotificationCategory(identifier: "VIEW_NOTIFICATION", //category name
actions: [acceptAction, skipAction],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)
// Register the notification type.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([viewCategory])
}
func viewNotification(_ userInfo: [AnyHashable : Any]) {
//handle extra data
let id = (userInfo["id"] as? String) ?? ""
let title = (userInfo["title"] as? String) ?? ""
let url = userInfo["url"] as? String
NotificationService.shared.viewNotification(id, title: title, url: url)
}
//MARK: UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// if user tap or response to not "skip" action we can handle here
let userInfo = response.notification.request.content.userInfo
if response.actionIdentifier != "skip" {
viewNotification(userInfo)
}
// Always call the completion handler when done.
completionHandler()
}