欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

phonegap -ios -push 消息推送实践(二)

ios 夜阑小雨 2833℃ 0评论

2.phonegpa ios客户端实现

[上一章我们已经把相应的配置文件及pem文件生成,现在我们开始配置客户端,客户端需要刚才通过配置push服务的ios开发证书,具体在xcode可里面怎么配置就不在说了].

1 2 3

获取后根据以下步骤来集成:

1.将plugins文件夹里面的文件 拖放复制到你的项目 plugins文件夹里面,

2.再你的config.xml引入插件:

<feature name=”PushNotification”>
<param name=”ios-package” value=”PushNotification”/>
</feature>

3.将 推送添加代码.txt 里面代码 合并到你的AppDelegate.m 里面 具体看文档

4.将 www 里面的js 引入你的 html 页面里面。

【特别说明】:
初始化插件 用 push.js 里面的 bindEvents() 方法。

在 push.js 的storeToken() 方法里面 修改你的url ,这个url是用来存储用户的token的。用于服务器的推送
当然前提是你必须先 把服务器代码上传到你的服务器,并且能访问到。
var deviceToken 定义有2个 开发的时候用目前这个,当你上传商店的时候 换成上面那个。删掉当前的。

方法 getPending 是用来获取推送消息后,跳转app 并做下一步骤操作的放大。 你可以再这个方法里面做后续的 显示推送信息和跳转页面的工作。

PushNotification.js 方法是 插件文件,里面需要修改 notificationCallback方法,这个方法是在当前app打开的情况下面显示推送消息的,具体业务逻辑,可以自己调整。

5.最终要的要实现推送必须 创建正确的推送证书,这个可以参考网站内容。

编写 插件调用方法

我这里写好了 我给大家分享哈

一个有几个方法,主要是 获取token值,然后通过ajax 传回服务器端,当然服务器端必须又接受的程序,再把值存储到sql里面。然后再服务器端通过改值来发送推送信息。

function bindEvents() {
document.addEventListener(‘deviceready’, onDeviceReadypush, false);
document.addEventListener(‘resume’, onResume, false);
}

function onResume() {
//app.myLog.value=””;
// Clear the badge number – if a new notification is received it will have a number set on it for the badge
setBadge(0);
getPending(); // Get pending since we were reopened and may have been launched from a push notification
}

function onDeviceReadypush() {
register(); // Call to register device immediately every time since unique token can change (per Apple)
// This will cause to fire when app is active already
document.addEventListener(‘push-notification’, function(event) { //º‡Ã˝–≈œ¢
console.log(‘RECEIVED NOTIFICATION! Push-notification! ‘ + event);
//app.myLog.value+=JSON.stringify([‘\nPush notification received!’, event]);
// Could pop an alert here if app is open and you still wanted to see your alert
navigator.notification.alert(“Received notification – fired Push Event ” + JSON.stringify([‘push-//notification!’, event])); //Ω” ‹µΩ“ªÃı–≈œ¢
});
document.removeEventListener(‘deviceready’, deviceready, false);
}

function setBadge(num) { // …Ë÷√badege÷µ
var pushNotification = window.plugins.pushNotification;
//app.myLog.value+=”Clear badge… \n”;
pushNotification.setApplicationIconBadgeNumber(num);
}

function receiveStatus() { //ªÒ»°Ω” ‹–≈œ¢◊¥Ã¨
var pushNotification = window.plugins.pushNotification;
pushNotification.getRemoteNotificationStatus(function(status) {
//app.myLog.value+=JSON.stringify([‘Registration check – getRemoteNotificationStatus’, status])+”\n”;
});
}

function getPending() {
var pushNotification = window.plugins.pushNotification;
pushNotification.getPendingNotifications(function(notifications) {
//app.myLog.value+=JSON.stringify([‘getPendingNotifications’, notifications])+”\n”;
console.log(JSON.stringify([‘getPendingNotifications’, notifications]));
});
}

function register() { //ªÒ»°devive token÷µ
var pushNotification = window.plugins.pushNotification;
pushNotification.registerDevice({alert:true, badge:true, sound:true}, function(status) {
//app.myLog.value+=JSON.stringify([‘registerDevice status: ‘, status])+”\n”;
storeToken(status.deviceToken);
//navigator.notification.alert(status.deviceToken);

});
}

function storeToken(token) { //∑¢ÀÕtokenµΩ∑˛ŒÒ∆˜
console.log(“Token is ” + token);
//navigator.notification.alert(token);
var devices_name = device.name;
var devices_version =device.version;
var devices_type =device.platform ;
var devices_token = token;
var deviceToken=’&devices_name=’+devices_name+’&devices_version=’+devices_version+’&devices_type=’+devices_type+’&devices_token=’+devices_token;
//navigator.notification.alert(deviceToken);
$.ajax({
type: “POST”,
url: “http://******/ios_push/pushtest/apns.php?action=registerDevices”,
data: deviceToken,
success: function(msg) {
//navigator.notification.alert(msg);
console.log(msg);
}

});
}

 

然后再index页面调用 bindEvents()方法,这样应用在运行是回执行改方法,把token值获取到

传回服务器,然后服务器来发送信息。

关于设置Badge数,这里必须再页面启动的js里面添加以下的方法来对Badge归0.

  document.addEventListener(‘resume’, onResume, false);

转载请注明:夜阑小雨 » phonegap -ios -push 消息推送实践(二)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 你好, 我按照你的教程实践了,xcode运行程序的时候, iphone显示 push notification 已经allowed, 在settings 里面也可以看到程序已经被添加到 允许发送push notifications 的列表zhong. 可是服务器端的devices表里面没有任何纪录,所以如果我如果在服务器端点击发送推送消息,iphone的程序没有任何响应。 我觉得可能是服务器端的pem文件没有设置好, 请问要如何设置呢?
    2013-08-12 01:12 回复