为啥我向主题发送 Firebase 通知的请求不起作用(HTTP POST)?
Posted
技术标签:
【中文标题】为啥我向主题发送 Firebase 通知的请求不起作用(HTTP POST)?【英文标题】:How come my request to send Firebase notification to a topic is not working (HTTP POST)?为什么我向主题发送 Firebase 通知的请求不起作用(HTTP POST)? 【发布时间】:2017-05-23 16:28:46 【问题描述】:非常感谢您对此的任何帮助.. 我要做的就是使用我的 php 代码向订阅“全球”主题的所有用户发送通知。有谁知道为什么它可能不起作用?因为我希望每个使用该应用程序的人都能收到通知,所以我会订阅每个人(除非有更好的方法)。这是我的 php 尝试将通知发送到我的全局主题:
<?php
define( 'API_ACCESS_KEY', 'hidden...hidden' );
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'to' => "/topics/global",
'data' => $msg,
'priority' => 'high'
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec( $ch );
curl_close( $ch );
echo $result;
?>
我缺乏知识,但从 $result 回显来看,它看起来不像任何失败消息。这是我得到的:
"message_id":7591682951632927615
在我的 Firebase 控制台中,我什至看不到“全局”主题,因此我无法测试发送到该主题的功能在我的设备上是否有效。从我在线阅读的内容来看,订阅的主题可能需要一段时间才能出现在控制台中。 澄清一下,在控制台中使用设置为应用程序的用户段向所有设备发送通知!
我可以做些什么来验证我的应用实际上是在为用户订阅“全球”主题吗?也许这就是问题所在。以下是相关的 swift 代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
FIRApp.configure()
if #available(ios 10.0, *)
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: _, _ in )
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
else
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage)
print("applicationReceivedRemoteMessage")
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
if let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
FIRMessaging.messaging().subscribe(toTopic: "/topics/global")
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
/*
// Print message ID.
if let messageID = userInfo["gcmMessageIDKey"]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
*/
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
if application.applicationState == UIApplicationState.active
print("GOT IN HERE")
var pushNotificationMessage = ""
if let aps = userInfo["aps"] as? NSDictionary
if let alert = aps["alert"] as? NSDictionary
if let message = alert["message"] as? NSString
pushNotificationMessage = message as String
else if let alert = aps["alert"] as? NSString
pushNotificationMessage = alert as String
let notificationAlert = UIAlertController(title: nil, message: pushNotificationMessage, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler:
(alert: UIAlertAction!) -> Void in
)
defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: "titleTextColor")
notificationAlert.addAction(defaultAction)
self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil)
【问题讨论】:
This documentation 使用略有不同的 URL:fcm
而不是 gcm
。
【参考方案1】:
要发送notification
,请将参数存储在notification
,而不是数据:
$fields = array
(
'to' => "/topics/global",
'notification' => $msg, // <= CHANGED
'priority' => 'high'
);
另请参阅Notification payload support 文档中的表 2a。不支持message
,请改用body
。
$msg = array
(
'body' => 'here is a message. message', // <= CHANGED
'title' => 'This is a title. title',
'vibrate' => 1,
'sound' => 1
);
【讨论】:
非常感谢。我将其保留为 gcm(因为 fcm 出于某种原因给了我 404 错误),但是在将其更改为通知后,它在某种程度上可以工作。我能够验证该主题是否有效,但奇怪的是我只听到了通知的声音。 解决了我的问题。非常感谢鲍勃。以上是关于为啥我向主题发送 Firebase 通知的请求不起作用(HTTP POST)?的主要内容,如果未能解决你的问题,请参考以下文章