我想在我的應用程序中集成一個小型聊天/即時消息功能,需要一點啓動幫助。 我已註冊Google Cloud Messaging帳戶並收到配置文件(「google-services.json」,包含項目ID)和服務器API密鑰。將通訊功能集成到Phonegap-App
第一個問題是:我必須在我的谷歌帳戶進行進一步的設置,以便該應用程序獲取所需的信息功能的所有信息?
接下來的問題是,如何在我的項目中集成消息傳遞功能。
我做了什麼(什麼似乎不工作):
在我的index.html文件,我已經將下面的代碼(XXXXXXXXX已替換爲我的發件人ID):
<script type="text/javascript" src="PushNotification.js"></script>
<script>
var pushNotification;
function onDeviceReady() {
alert('Device is ready');
try {
pushNotification = window.plugins.pushNotification;
alert('Registering ' + device.platform);
if(device.platform == 'android' || device.platform == 'Android' ||device.platform == 'amazon-fireos') {
pushNotification.register(
successHandler,
errorHandler,
{
"senderID":"XXXXXXXXXXX",
"ecb":"onNotification"
});
alert('Registered the Android device');
alert('regID = ' + e.regid);
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
alert('Registered the iOS device');
}
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
//alert(txt);
alert('Error: ' + err.message);
}
}
// handle APNS notifications for iOS
function onNotificationAPN(e) {
if(e.alert) {
// showing an alert also requires the org.apache.cordova.dialogs plugin
navigator.notification.alert(e.alert);
}
if(e.sound) {
// playing a sound also requires the org.apache.cordova.media plugin
var snd = new Media(e.sound);
snd.play();
}
if(e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android
function onNotification(e) {
alert('EVENT -> RECEIVED:' + e.event);
switch(e.event)
{
case 'registered':
if(e.regid.length > 0)
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log("regID = " + e.regid);
alert(' REGID = ' + e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if(e.foreground) {
alert('--INLINE NOTIFICATION--');
// on Android soundname is outside the payload.
// On Amazon FireOS all custom attributes are contained within payload
var soundfile = e.soundname || e.payload.sound;
// if the notification contains a soundname, play it.
// playing a sound also requires the org.apache.cordova.media plugin
var my_media = new Media("/android_asset/www/"+ soundfile);
my_media.play();
} else {
// otherwise we were launched because the user touched a notification in the notification tray.
if(e.coldstart)
//$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
alert('--COLDSTART NOTIFICATION--')
else
alert('--BACKGROUND NOTIFICATION--')
}
alert('MESSAGE -> MSG: ' + e.payload.message);
//android only
alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
//amazon-fireos only
alert('MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp);
break;
case 'error':
//$("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
alert('ERROR -> MSG' + e.msg);
break;
default:
//$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
alert('EVENT -> Unknown, an event was received and we do not know what it is');
break;
}
}
function tokenHandler (result) {
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
alert('iOS Result = ' + result);
}
function successHandler (result) {
alert('Android Result = ' + result);
alert('RegID = ' + e.regid);
}
function errorHandler (error) {
alert('Error = ' + error);
}
document.addEventListener('deviceready', onDeviceReady, true);
alert('regID = ' + e.regid);
alert('Reg code completed');
</script>
,因爲我用Phonegab建設,我已經添加了下面一行到我的config.xml文件
<gap:plugin name="com.phonegap.plugins.pushplugin" version="2.4.0" />
下面的插件(也使用)已經在配置文件中有一部分:
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.media" />
我的主要問題是:
我在哪裏必須把服務器API密鑰? 我在哪裏必須放置google-services.json文件(在哪個目錄中)以及該文件如何鏈接到他index.html
更多問題: - GCM服務真的是免費的嗎?因爲在會員區有一個顯示「本月預計費用:0,00€」 - >費用是否會增加? - 如果用戶從PlayStore下載並安裝我的應用:他如何獲得發件人ID,如果我想給他發送消息 - >我如何知道他的發件人ID以開始與他通信?
原則上我還沒有理解GCM東西。 任何人都可以給我一點幫助嗎?
問候
丹尼爾