使用PHP发件人和Swift

时间:2015-07-13 17:53:48

标签: php ios notifications

我正致力于通过GCM获取后台通知以使用IOS - 非后台通知已经正常工作。以下是我整合背景通知的步骤:

  1. 在UIBackgroundmodes
  2. 中启用remote-notifications标记
  3. 将内容可用密钥添加到我的通知有效负载中。
  4. 编写应用程序:didRecieveRemoteNotification:fetchCompletionHandler:在我的委托中。
  5. 以下是委托功能的代码:

    func application( application: UIApplication,
        didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
        fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
            println("Notification received2: \(userInfo)")
    
            GCMService.sharedInstance().appDidReceiveMessage(userInfo);
    
            NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
                userInfo: userInfo)
            handler(UIBackgroundFetchResult.NoData);    
    }
    

    这是on-server php脚本的代码:

    <?php
    $regId = $_GET["regId"];
    $message = $_GET["message"];
    
    $data = array( 'price' => $message, 'sound' => 'default', 'body' => 'helloworld', 'title' => 'default', 'badge' => 12, 'content-available' => 1);
    
    $ids = array( $regId);
    
    sendGoogleCloudMessage(  $data, $ids );
    
    function sendGoogleCloudMessage( $data, $ids )
    {
    
        $apiKey = THE-API-KEY-THAT-I-AM-USING;
    
        $url = 'https://android.googleapis.com/gcm/send';
    
        $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data,
                    'content-available' => 1,
        );
    
        $headers = array(
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );
    
        $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, json_encode( $post ) );
    
        $result = curl_exec( $ch );
    
        if ( curl_errno( $ch ) )
        {
            echo 'GCM error: ' . curl_error( $ch );
        }
    
        curl_close( $ch );
    
        echo $result;
    }
    ?>
    

    我尝试通过内部&#34;数据&#34;发送内容可用标志。数组和外部&#34; post&#34;数组,我已将其添加到两者中。

    fetchCompletionHandler函数未收到该消息,而是等待应用程序再次处于活动状态并被正常应用程序接收:didRecieveRemoteNotification函数。可能是因为没有通过后台功能收到我的通知?

1 个答案:

答案 0 :(得分:2)

您应该向GCM提供 content_available ,而不是内容可用(例如在APN中),您将能够在后台接收推送通知。

https://developers.google.com/cloud-messaging/server-ref#send-downstream

P.S。 - 不到五分钟前,我遇到了同样的问题。我们应该更仔细地阅读文档......