FCM通过fcm控制台向我的ios设备发送通知,但是从php服务它没有发送通知

时间:2016-08-26 09:06:58

标签: php firebase push-notification apple-push-notifications firebase-cloud-messaging

FCM通过fcm控制台向我的ios设备发送通知,但是从php服务它没有发送通知。我想使用FCM向我的应用发送通知。我已经在php中实现了web服务,以便在我的应用服务器上向app发送消息。我为此创建了4个服务。

  1. group_create.php
  2. device_add.php
  3. device_remove.php
  4. send_comments.php
  5. 创建组后,我成功获得了通知密钥,并使用fcm注册了registraion id。当我用通知键调用send_comments.php时,它返回json数据{{#34;成功":1,"失败":0}。但我没有收到关于我的ios的任何通知。我已正确实施所有方法。它适用于fcm控制台,但不适用于php服务。任何人都可以知道这一点。我附加了所有4个PHP文件。请帮帮我。

    group_create.php

    <?php 
    $url = 'https://android.googleapis.com/gcm/notification';
    
    $notification_key_name = $_REQUEST['notification_key_name'];
    $regid = $_REQUEST['regid'];
        $fields = array(
           "operation"=>"create",
           "notification_key_name"=>$notification_key_name,
           "registration_ids"=> array($regid)
    );
    $fields = json_encode( $fields );
    
    $headers = array (
            "Authorization:key=A************************",
            "Content-Type:application/json",
            "project_id:78508******"
    );
    
    $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_POSTFIELDS, $fields );
    
    $result = curl_exec ( $ch );
    
    //echo $result;
    $res_notification_key = json_decode($result,true);
    
     if(array_key_exists('notification_key', $res_notification_key)){
    
    $notification_key = $res_notification_key['notification_key'];
    
     echo $notification_key;
    
    }
    
    else{
    
    echo $result;
    
    }
    curl_close ( $ch );
    ?>
    

    device_add.php

    <?php
    
    $senderId = "785********";
    $notification_key_name= $_REQUEST['notification_key_name'];
    $reg_id = $_REQUEST['regid'];
    $notification_key = $_REQUEST['not_key'];
    $apiKey = 
    
    $url = 'https://android.googleapis.com/gcm/notification';
    
      $headers = array (
            "Accept:application/json",
            "Authorization:key=A******************",
            "Content-Type:application/json",
            "project_id:78508*****"
    );
    
    
     $fields = array(
           "operation"=>"add",
           "notification_key_name"=> $notification_key_name,
           "registration_ids"=> array($reg_id),
           "notification_key"=>$notification_key
    );
    $fields = json_encode( $fields );
    
     $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_POSTFIELDS, $fields );
    
    
    $result = curl_exec ( $ch );
    
    echo $result;
    $res_notification_key = json_decode($result,true);
    
    if(array_key_exists('notification_key', $res_notification_key)){
    
    
    $notification_key = $res_notification_key['notification_key'];
    
     echo $notification_key;
    
    }
    
    else{
    
    echo $result;
    
    }
    curl_close ( $ch );
    ?>
    

    device_remove.php

    <?php
    
    $senderId = "78508*****";
    $notification_key_name= $_REQUEST['notification_key_name'];
    $reg_id = $_REQUEST['regid'];
    $notification_key = $_REQUEST['not_key'];
    $apiKey = 
    
    $url = 'https://android.googleapis.com/gcm/notification';
    
      $headers = array (
            "Accept:application/json",
            "Authorization:key=A***********",
            "Content-Type:application/json",
            "project_id:78508*****"
    );
    
    
     $fields = array(
           "operation"=>"remove",
           "notification_key_name"=> $notification_key_name,
           "registration_ids"=> array($reg_id),
           "notification_key"=>$notification_key
    );
    $fields = json_encode( $fields );
    
     $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_POSTFIELDS, $fields );
    
    
    $result = curl_exec ( $ch );
    
    echo $result;
    $res_notification_key = json_decode($result,true);
    
    
    
    if(array_key_exists('notification_key', $res_notification_key)){
    
    
    $notification_key = $res_notification_key['notification_key'];
    
     echo $notification_key;
    
    }
    
    else{
    
    echo $result;
    
    }
    curl_close ( $ch );
    ?>
    

    send_comments.php

    <?php
    
    $senderId = "78508*****";
    $notification_key = $_REQUEST['not_key'];
    
    $url = 'https://fcm.googleapis.com/fcm/send';
    
      $headers = array (
            "Authorization:key=A*****************",
            "Content-Type:application/json",
    
    
    );
    
    $msg = array("hello"=>"This is a Firebase Cloud Messaging Device Group Message!");
    
    $msg_dict = json_encode($msg);
    
    //echo $msg_dict;
    
     $fields = array(
           "to"=>$notification_key,
           "data"=>array(
                "message" => "hell",
    
        ),
    );
    $fields = json_encode( $fields );
    
     $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_POSTFIELDS, $fields );
    
    $result = curl_exec ( $ch );
    
    echo $result;
    $res_notification_key = json_decode($result,true);
    
    curl_close ( $ch );
    ?>
    

1 个答案:

答案 0 :(得分:0)

APN遵循格式

{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}}

即,

{
"aps":{
"alert":"message here"
}
}

如果从服务器端发送的格式是这样,那么只有iPhone会显示通知,其他明智的通知数据只能通过控制台打印

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage         *)remoteMessage {
// Print full message
NSLog(@"Notification :%@", remoteMessage.appData);
}

但不会显示在通知列表中。