详细请看这个

http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

 

一、 获取到gcm 需要的Google API project 的 Project Number 和 Server key.

详细内容看这里:http://developer.android.com/google/gcm/gs.html

 

1.创建Google API project(获取到Project Number);

 

2. 让 GCM Service 运行;

 

3. 获得一个Server key (服务器上用到);

                 

二、回到你的控制台;

1.运行 : cordova create pushtest  —id “com.debbie.push” —name “pushtest”

2.运行 :cd pushtest

3.运行 :cordova platform add android

4.运行 :cordova plugin add https://github.com/phonegap-build/PushPlugin

5.把pushtest/plugins/com.phonegap.plugins.PushPlugin/www/PushNotification.js 文件拷贝到

pushtest/www/js/ 下

6.在你的index.html 中添加引用PushNotification.js

<script type="text/javascript" src=“PushNotification.js"></script>

7. 在你的pushtest/www/js/index.js 中添加一下的代码;

 1 /*
 2  * Licensed to the Apache Software Foundation (ASF) under one
 3  * or more contributor license agreements.  See the NOTICE file
 4  * distributed with this work for additional information
 5  * regarding copyright ownership.  The ASF licenses this file
 6  * to you under the Apache License, Version 2.0 (the
 7  * "License"); you may not use this file except in compliance
 8  * with the License.  You may obtain a copy of the License at
 9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 var app = {
20     // Application Constructor
21     initialize: function() {
22         this.bindEvents();
23     },
24     // Bind Event Listeners
25     //
26     // Bind any events that are required on startup. Common events are:
27     // 'load', 'deviceready', 'offline', and 'online'.
28     bindEvents: function() {
29         document.addEventListener('deviceready', this.onDeviceReady, false);
30     },
31     // deviceready Event Handler
32     //
33     // The scope of 'this' is the event. In order to call the 'receivedEvent'
34     // function, we must explicitly call 'app.receivedEvent(...);'
35     onDeviceReady: function() {
36         app.receivedEvent('deviceready');
37          /*
38         在 onDeviceReady 函数中添加注册推送信息;
39         其中senderID 是在第一大点那里的 Project Number 
40         如果注册失败则执行 app.errorHandler;
41         如若注册成功 则执行 app.successHandler;并且执行成功回调函数 app.onNotificationGCM ,返回手机的注册号e.regid
42          */
43         var pushNotification = window.plugins.pushNotification;
44          pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"139669249027","ecb":"app.onNotificationGCM"});
45     },
46     // Update DOM on a Received Event
47     receivedEvent: function(id) {
48         var parentElement = document.getElementById(id);
49         var listeningElement = parentElement.querySelector('.listening');
50         var receivedElement = parentElement.querySelector('.received');
51 
52         listeningElement.setAttribute('style', 'display:none;');
53         receivedElement.setAttribute('style', 'display:block;');
54 
55         console.log('Received Event: ' + id);
56        
57     },
58     // result contains any message sent from the plugin call
59     successHandler: function(result) {
60         alert('Callback Success! Result = '+result)
61     },
62     errorHandler:function(error) {
63         alert(error);
64     },
65     onNotificationGCM: function(e) {
66         switch( e.event )
67         {
68             case 'registered':
69                 if ( e.regid.length > 0 )
70                 {
71                     console.log("Regid " + e.regid);
72                     alert('registration id = '+e.regid);
73                 }
74                 break;
75 
76             case 'message':
77                 // this is the actual push notification. its format depends on the data model from the push server
78                 alert('message = '+e.message+' msgcnt = '+e.msgcnt);
79                 break;
80 
81             case 'error':
82                 alert('GCM error = '+e.msg);
83                 break;
84 
85             default:
86                 alert('An unknown GCM event has occurred');
87                 break;
88         }
89     }
90 };
91 
92 app.initialize();
View Code

 

三、

运行 :cordova build android

如果成功,将弹出一个注册号的对话框;通过Eclipse的控制台 获取到注册号;如

四、

运行服务器;

运行: node pushtest

手机收到推送信息;

 

 

其中pushtest.js的具体内容如下;

var gcm = require('node-gcm');
var message = new gcm.Message();
 
//API Server Key

var sender = new gcm.Sender('AIzaSyBRfgV0W8kpNcvJXiFB5SqtMRF4-O7mWNE');  //pushtest
var registrationIds = [];
 
// Value the payload data to send...
message.addData('message',"hello ebookingxxxxx");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
//message.collapseKey = 'demo';
//message.delayWhileIdle = true; //Default is false
message.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
 
// At least one reg id requiredAPA
registrationIds.push('APA91bECPd1Ygrsk-DwTGnVhyGgjpv3TGpzNVhJHrZ85GwURE-nArWEc3qcppD_ox9wXbNy17C_5L5xzsKAZtVGoXdBVjkEGjtcfA6HrjetA74lhSjvEDvzmf6Um2PY50g0KteNqAP5NH9HdhkuqYpQoxf8FMQHB9A');
 
/**
 * Parameters: message-literal, registrationIds-array, No. of retries, callback-function
 */
sender.send(message, registrationIds, 4, function (result) {
    console.log(result);
});
View Code

 

posted on 2015-01-16 16:03  ทดสอบ  阅读(443)  评论(0)    收藏  举报