将消息传递功能集成到Phonegap-App中

时间:2015-07-22 05:12:59

标签: android google-cloud-messaging

我想在我的应用中集成一个小型聊天/即时消息功能,需要一些启动辅助工具。 我已注册Google云消息传递帐户并收到配置文件(" google-services.json",其中包含项目ID)和服务器API密钥。

第一个问题是:我是否必须在我的Google帐户中进行进一步设置,以便应用获取讯息功能所需的所有信息?

接下来的问题是,如何在我的项目中集成messenging函数。

我做了什么(以及什么似乎不起作用):

在我的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 Build时,我已将以下行添加到我的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" />

我的主要问题是:

我在哪里放置Server API密钥? 我在哪里放置google-services.json文件(在哪个目录中)以及该文件如何链接到index.html

进一步的问题: - GCM服务真的免费吗?因为在会员区有一个展示&#34;本月的预计费用:0,00€&#34; - &GT;费用会增加吗? - 如果用户从Play商店下载并安装我的应用程序:他如何获得发件人ID以及我是否要向他发送消息 - &gt;我怎么知道他的发件人ID才能开始与他交流?

原则上我还没有理解GCM这个东西。 谁能给我一点帮助?

祝你好运

丹尼尔

1 个答案:

答案 0 :(得分:0)

GCM服务本身是免费的,如here所述。

我不熟悉设置Phonegap项目,但google-services.json文件旨在与Gradle的Google服务插件一起使用。 Gradle本身是Android Studio使用的构建工具。我不确定Phonegap是否有类似的插件,但如果没有,你需要的是项目编号/发件人ID。您注册了您的应用程序/客户端。更多内容如下。

您在服务器代码中使用服务器API密钥,该服务器代码是将向您的应用发送消息的应用服务器。

更多关于注册的信息。您需要查看一些Phonegap教程,因为GCM的新(和推荐)注册方式是Instance IDs。它仍然使用相同的发件人ID,但您需要与Google Play服务进行交互才能执行此操作。