[英]Sending push notification from php to android app
使用Ionic框架作為前端和php作為后端在android應用程序開發中工作。 有人可以建議您的觀點如何實現這一目標。 准備好使用示例應用程序(遵循以下步驟)1.使用以下注釋創建示例應用程序
ionic start devdactic-android-push
cd devdactic-android-push
ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push
ionic io init
2.更新了app.js,如下所示:$ ionicPlatform.ready(function(){
$ionicPlatform.ready(function() {
var push = new Ionic.Push({
"debug": true
});
push.register(function(token) {
console.log("Device token:",token.token);
});
});
});
運行以下命令以獲取設備令牌
離子發球
我可以在cosole中獲取設備令牌
我需要獲取真正的android手機的設備令牌,因此准備了apk文件
ionic平台添加android ionic構建android
從控制台獲取真實設備的設備令牌(使用chrome inspector檢查android應用程序控制台)
在Google雲端平台中創建了一個項目,因此我同時擁有了項目編號(GCM控制台)和設備令牌
項目ID-257581368411設備令牌-DEV-e51b469d-9024-4d88-a0a9-1147f45b13f4
如何使用上述值從PHP腳本發送通知?
以下是我的系統信息:
Cordova CLI: 6.1.1
Ionic Version: 1.2.4
Ionic CLI Version: 1.7.14
Ionic App Lib Version: 0.7.0
OS: Windows 7 SP1
Node Version: v5.0.0
您可以使用下面的腳本發送推送通知
資料來源: https : //gist.github.com/prime31/5675017
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['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'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.