gpt4 book ai didi

php - 结合 ios (Push notification) 和 android (GCM) API (webservice) 进行通知

转载 作者:行者123 更新时间:2023-11-29 12:00:42 24 4
gpt4 key购买 nike

我是一名 Android 开发人员,所以我对 PHP 了解不多。

我们正在开发 Android 和 IOS 试卷应用程序。并创建它完美运行的通知功能。但问题是 IOS 使用推送通知而 android 使用 GCM。而这两个是使用不同的api。

我的问题是如何结合这两个 api。我想将这两个 api (webservice) 合二为一。当管理员上传论文时,所有用户都会使用通知天气收到通知,它是 android 或 IOS 设备。

我该怎么办?非常感谢提前致谢。我正在使用这个 LINK

这是我的努力

Android API

 <?php

require_once('connect.php');
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyBWcc6ONXvNEHECxzLwFDSDCTKDTjEhfT8' );
//$registrationIds = array( $_GET['id'] );

$conn = new mysqli('host name', 'server name', 'password', 'database name');

$select_qury = "SELECT gcm_registration_id FROM gtu_registration";
$result = $conn->query($select_qury);

$registrationIds = array();

while($row = $result->fetch_assoc()){

array_push($registrationIds, $row['gcm_registration_id']);
}
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText'=> 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);

//Creating a new array fileds and adding the msg array and registration token array here
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);

//Adding the api key in one more array header
$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 );

//Decoding json from result
$res = json_decode($result);

$flag = $res->success;

//if success is 1 means message is sent
if($flag == 1){
//Redirecting back to our form with a request success
header('Location: index.php?success');
}else{
//Redirecting back to our form with a request failure
header('Location: index.php?failure');
}

echo $result;

IOS API

<?php
$token ='f1088d1c0603218b31b5a26210c4b0e60e4884225235c7f5c2e55c47b937a03d';
$deviceToken = $token; // token dell'iPhone a cui inviare la notifica

// Passphrase for the private key (ck.pem file)
$pass = "mac12345";
// Get the parameters from http get or from command line
$message = 'Notification text';
$badge = 1;
$sound = 'default';

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);

if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;

/ End of Configurable Items /
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl','local_cert', 'Gtuck.pem');

// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 120, STREAM_CLIENT_CONNECT, $ctx);


$payload = json_encode($body);
$msg = chr(0).pack('n',32).pack('H*', str_replace(' ', '', $deviceToken)).pack('n',strlen($payload)).$payload;
print "" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>

可不可以??如果有任何建议或其他方式来完成这项工作,请告诉我。

最佳答案

下面的代码是我自己用的,你可以改成自己的方式

根据设备操作系统

if($gcm_id_array['os'] == 'android')
{
$v[$index] = scs_single_gcm($gcm_id_array['gcm_id'],$message);
$index++;
}else{
$v[$index] = scs_single_gcm_ios($gcm_id_array['gcm_id'],$message);
$index++;
}

iOS scs_single_gcm_ios 的功能

function  scs_single_gcm_ios($regids,$message)
{
try{
$deviceToken = $regids;
$passphrase = 'Kookdokoo';
$title = $message['title'];
$id = $message['noti_id'];

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);


$body['aps'] = array(
'alert' => $title,
'sound' => 'default',
'id' =>$id
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
$result = 0;
else
$result = 1;

// Close the connection to the server
fclose($fp);
return $result;
}
catch(Exception $e){
$e->getMessage();
}
}

安卓

function scs_single_gcm($regids,$message)
{


if(is_array($regids)){
$fields = array(
'registration_ids' => $regids,
'data' => array( "data" => $message ),
);

}else
{
$fields = array(
'registration_ids' => array($regids),
'data' => array( "data" => $message ),
);

}
// Open connection
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt( $ch, CURLOPT_URL,url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER,array(key,content_type));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
/*
if (FALSE === $result)
echo curl_error($ch).curl_errno($ch);
// Close connection
curl_close($ch);
if (curl_errno( $ch ) ){
'GCM error: ' . curl_error( $ch );
}
*/
$result = json_decode($result);

$notification = 1;
if($result->success){
$nm = 'push notification success';
$p = 1;
}else{
if($result->results[0]->error == "NotRegistered")
{
$nm = 'push notification success';
$p = 1;
}else{
$nm = 'push notification failed';
$p = 0;
}

}

关于php - 结合 ios (Push notification) 和 android (GCM) API (webservice) 进行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37135091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com