【问题标题】:Push Notification PhoneGap推送通知 PhoneGap
【发布时间】:2014-12-15 09:45:56
【问题描述】:

我正在开发一个应用程序来接收推送通知,但是我遇到了一些问题。

我正在使用 PhoneGap 开发我的应用程序和这个插件 - PushPlugin 我已经使用 PhoneGap 成功安装了插件,它在我安装的插件中​​列出,并且在我的手机上安装我的应用程序时我也拥有所需的权限。

我已经为我的应用注册了 GCM 服务,并获得了我的项目编号、API 密钥等。我按照此处的步骤操作 - Google Cloud Messaging

当应用第一次启动时,我显然需要为我的设备(顺便说一下是 Android 设备)获取一个注册 ID,但是我无法让它运行整个注册代码。

在插件的文档中,它说需要在设备准备好后立即调用代码 - 我已经这样做了。

var pushNotification;

document.addEventListener("deviceready", function(){
    pushNotification = window.plugins.pushNotification;

    $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
    pushNotification.register(
    successHandler,
    errorHandler,
    {
        "senderID":"sender_ID",
        "ecb":"onNotification"
    });
} 

});



// result contains any message sent from the plugin call
function successHandler (result) {
    alert('result = ' + result);
}

// result contains any error description text returned from the plugin call
function errorHandler (error) {
    alert('error = ' + error);
}

// Android and Amazon Fire OS
function onNotification(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

    switch( e.event )
    {
    case 'registered':
        if ( e.regid.length > 0 )
        {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
            // 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);
        }
    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 )
        {
            $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

            // 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.
            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>');
            }
            else
            {
                $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }

       $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
           //Only works for GCM
       $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
       //Only works on Amazon Fire OS
       $status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
    break;

    case 'error':
        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
    break;

    default:
        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
    break;
  }
}

如果成功,它应该运行成功处理程序,如果失败,则运行错误处理程序。但是我什么都没有。

我一直在缩小代码范围,以查看它是否在某行崩溃,使用警报。如下图——

alert('Start of push');
document.addEventListener("deviceready", function(){
var pushNotification;
    pushNotification = window.plugins.pushNotification;
    alert('past pushNotification');

    alert(device.platform);

    $("#app-status-ul").append('<li>registering ' + device.platform + '</li>'); //crashing on this line

    alert('registering');
if( device.platform == 'android' || device.platform == 'Android'){
    alert('pushreg');
    pushNotification.register(
    successHandler,
    errorHandler,
    {
        "senderID":"sender_ID_here",
        "ecb":"onNotification"
    });
} 
alert('register complete');    
});




// result contains any message sent from the plugin call
function successHandler (result) {
    alert('result = ' + result);
}

// result contains any error description text returned from the plugin call
function errorHandler (error) {
    alert('error = ' + error);
}

// Android and Amazon Fire OS
function onNotification(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

    switch( e.event )
    {
    case 'registered':
        if ( e.regid.length > 0 )
        {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
            // 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);
        }
    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 )
        {
            $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

            // 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.
            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>');
            }
            else
            {
                $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }

       $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
           //Only works for GCM
       $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
       //Only works on Amazon Fire OS
       $status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
    break;

    case 'error':
        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
    break;

    default:
        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
    break;
  }
}
alert('end of push');

显然它能够运行第一个警报“开始推送”以及“过去的推送通知”警报。我检查以确保device.platform 没有崩溃,并使用相同的代码发出警报——这会在我的设备上返回 Android。但是在那之后我似乎再也没有得到任何东西,这让我相信它在$("#app-status-ul").append('&lt;li&gt;registering ' + device.platform + '&lt;/li&gt;');上崩溃了

如果有人能帮我弄清楚这将是一个很大的帮助,

谢谢。

【问题讨论】:

    标签: android push-notification google-cloud-messaging phonegap-pushplugin


    【解决方案1】:

    要获取设备平台,应在您的 Phonegap/Cordova 应用上安装“Device”插件。

    您可以像这样从命令行向您的应用添加插件

    cordova plugin add org.apache.cordova.device
    

    列出你的插件

    cordova plugin ls
    

    如果您的列表中出现 ['org.apache.cordova.device'],则表示您的插件未安装

    删除插件

    cordova plugin rm org.apache.cordova.device
    

    【讨论】:

    • 嗨,我已经安装了所有依赖的插件。设备和媒体。
    • 尝试删除设备插件并重新安装。如果问题仍然存在,请尝试从应用中卸载“Push Plugin”并重新安装。
    【解决方案2】:

    这是构建推送通知应用程序所需遵循的步骤:

    *使用CLI安装cordova并运行以下命令(参考link

    1)创建一个cordova项目

    cordova create hello com.example.hello HelloWorld
    

    2)添加平台

    cordova platform add android
    

    3)构建项目

    cordova build android
    

    4)添加插件

    cordova plugin add org.apache.cordova.device
    cordova plugin add org.apache.cordova.media
    cordova plugin add https://github.com/phonegap-build/PushPlugin.git
    

    5)构建项目

    cordova build android
    

    6)在Eclipse中导入创建的项目

    7) 使用来自this 位置的所需代码来设计您的 index.html。

    8)从

    复制PushNotification.js

    ProjectFolderPath\plugins\com.phonegap.plugins.PushPlugin\www

    ProjectFolderPath - 使用 CLI 创建的项目的路径

    到你的eclipse项目assets/www/文件夹

    9)还在 assets/www 文件夹中添加一个 jquery .js 文件和一个声音文件。 (可以从Github示例文件夹下载复制)

    10) 在 index.html 中不要忘记更改 GCM 项目的 senderID(即 ProjectId)

    11) 在实际设备上运行您的应用。它可能不适用于模拟器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多