2017-06-06 69 views
1

我正在构建api以在YouTube频道上上传视频。其中一项要求是,无论何时将视频成功上传到YouTube服务器,都必须使用Firebase云消息传递将通知发送到ios应用程序包ID。 我已经注册了我的FCM控制台应用程序与捆绑ID com.tbox.ygtp 我试图谷歌如何发送通知给iOS应用,我发现下面的API使用FCM将通知发送到ios应用程序包ID

$url = 'https://fcm.googleapis.com/fcm/send'; 
    $server_key = 'AAAAylThdyA:APA91bGISSU********VTqHJqBnV81B3jfJzAice07E********HHLXTVylMU1OWSHvdyoZMVNlM8t9p9tXH_4OKm********fEOatp_alw9qMlQNT507lLWLt1N_FMHel1JCCWcuQjD'; 

    $fields = array(); 
    //$fields['data'] = $data; 
    $fields['notification'] = array('body'=>$data); 
    if(is_array($target)){ 
     $fields['registration_ids'] = $target; 
    }else{ 
     $fields['to'] = $target; 
    } 
    //header with content_type and api key 
    $headers = array(
     'Content-Type:application/json', 
     'Authorization:key='.$server_key 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
    $result = curl_exec($ch); 
    if ($result === FALSE) { 
     die('FCM Send Error: ' . curl_error($ch)); 
    } 
    curl_close($ch); 

    return $result; 

这个API通知发送给设备令牌。但是我需要通过应用程序捆绑ID发送通知。 有人可以指导我如何使用FCM将通知发送到应用程序包ID。

回答

0

目前没有API或参数可用于将消息仅发送到特定的包ID。此选项仅在通过Firebase Notifications Console发送邮件时可用。

作为解决方法,您可以使用Topic Messaging,通过向用户订阅主题(您可以将其命名为捆绑ID或更简单的别名),然后只需使用主题名称将消息发送给用户。

+1

昨天还可以看到我对类似问题的回答:https://stackoverflow.com/questions/44364394/how-to-send-firebase-message-to-specific-application-in-project/44364798#44364798 –

+0

谢谢Puf。这样做会更好吗?(因为这个想法几乎相同)? –

+1

我认为把答案分开是很好的做法。我只是想把它们联系起来,因为正如你所说的那样解决方案是相似的。 –

相关问题