推送通知的PhoneGap [英] Push Notification PhoneGap

查看:202
本文介绍了推送通知的PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序来接收推送通知,但我有一些问题。

I am developing an app to receive push notifications, however I am having a few problems.

我使用PhoneGap的开发我的应用程序和这个插件 - PushPlugin
我已经安装使用PhoneGap的成功的插件,它是在我安装的插件上市及我的手机上安装我的应用程序时,我也有需要的权限。

I am using PhoneGap to develop my app and this plugin - PushPlugin I have installed the Plugin using PhoneGap successfully and it is listed in my installed plugins and I also have the needed permissions when installing my app on my phone.

我已经注册我的应用程序的GCM服务,并已取得我的项目数,API密钥等。我按照步骤在这里 - 的谷歌云消息

I have registered my app for GCM services and have obtained my Project Number, API key etc. I followed the steps here - Google Cloud Messaging.

当应用程序第一次启动时我显然需要得到一个注册ID为我的设备(顺便说一下Android设备),但是我一直没能得到它运行整个注册code。

When the app first starts I will obviously need to get a registration ID for my device (Android device by the way), however I haven't been able to get it to run the entire registration code.

在该插件的文件它说,code需求为设备准备尽快叫 - 这是我做的事。

In the documentation for the plugin it says the code needs to be called as soon as the device is ready - which I have done.

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;
  }
}

在成功应该运行成功处理程序,或者如果它失败,错误处理程序。不过我越来越没有。

On success it should run the success handler or if it fails, the error handler. However I am getting neither.

我已经收窄code下来,看它是否崩溃在一定行,用警报。如下图所示 -

I have been narrowing the code down to see if it is crashing on a certain line, using alerts. As shown below -

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 并没有崩溃,并把警报在同一code - 返回的Andr​​oid设备上。不过,我似乎从来没有经过任何东西,这使我相信,它的崩溃对 $(#应用程序状态-UL)追加(。'&LT;李&GT;注册'+ device.platform +'&LT; /李&GT;');

Obviously it is able to run the first alert "Start of Push" as well as the "past Push Notification" alert. I checked to make sure that the device.platform wasn't crashing it and put an alert in with the same code - which returns Android on my device. However I never seem to get anything after that, which leads me to believe that it's crashing on $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');

如果有人可以帮助我试着想出解决办法,这将是一个很大的帮助,

If anyone could help me try and figure this out that would be a great help,

感谢。

推荐答案

这是您需要按照建立一个应用程序的推送通知的步骤:

This are the steps you need to follow to build a App for push notification:

*安装使用科尔多瓦CLI并运行以下命令(参见<一个href=\"http://cordova.apache.org/docs/en/4.0.0/guide_cli_index.md.html#The%20Command-Line%20Interface\"相对=nofollow>链接)

*Install cordova using CLI and run following commands(refer link)

1)新建一个项目科尔多瓦

1)Create a cordova project

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中创建项目

6)Import the created project in Eclipse

7)使用需要code从位置来设计您的index.html。

7)Use required code from this location to design your index.html.

8)复制 PushNotification.js

ProjectFolderPath \\插件\\ com.phonegap.plugins.PushPlugin \\ WWW

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

ProjectFolderPath 项目-path使用CLI创建的

ProjectFolderPath -path of the project created using CLI

到Eclipse项目资产/ WWW / 文件夹

to your eclipse project assets/www/ folder

9),同时添加一个jQuery .js文件,并在资产/ WWW文件夹中的声音文件。 (你可以下载并 Github上示例文件夹)

9)Also add a jquery .js file and a sound file in assets/www folder. (you can download and copy it from Github Example folder)

10)的index.html不要忘记改变你的GCM项目(即专案编号)的senderID

10) In index.html do not forget to change senderID of your GCM project (i.e ProjectId)

11)运行您的实际设备上的应用程序。它可能无法在模拟器上工作。

11)Run your app on actual device. It might not work on Emulator.

这篇关于推送通知的PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆