首页 > 代码库 > phonegap + 极光推送 + 页面跳转 (ios)

phonegap + 极光推送 + 页面跳转 (ios)

安卓的解决办法请参见这里:安卓解决办法

ios如果没有页面跳转的需求的话就使用极光推送,如果有页面跳转如果用了极光推送就只有用oc去写,但是我不会oc,所以智能放弃极光选用ios自己的apns来实现

新建项目跟安卓创建项目差不多,新建完成后就是初始话phonegap,注意在phonegap.local.properties文件中phonegap.platform设置为ios

cmd移动到新建的项目的phonegap的文件夹下面安装插件:

phonegap local plugin add https://github.com/phonegap-build/PushPlugin.git
安装完插件后我们就可以在app.js文件夹中编写逻辑代码了。主要的逻辑代码写在app.js中的lunch方法中,写在外面会找不到对象

                plugins.pushNotification.register(
                                                  tokenHandler,
                                                  errorHandler,
                                                  {
                                                  "badge":"true",
                                                  "sound":"true",
                                                  "alert":"true",
                                                  "ecb":"onNotificationAPN"
                                                  });
tokenHandler是获取token的方法:

function tokenHandler (result) {
    alert('token = ' + result);
}
获得的这个token非常的重要,后面自己实现的apns推送需要这个token来标明推送到哪台设备上

errorHandler为错误的回调,可以在这里提醒下用户获取推送失败什么的

function errorHandler (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.
}

我这里没有写逻辑

onNotificationAPN:这个东西就是非常重要了,这个就是点击了推送后的回调函数

function onNotificationAPN(event){    
    if ( event.alert )
    {
        Ext.getCmp('tab').setActiveItem(1);
    }  
}
这个方法里面就可以写跳转页面的逻辑代码,我这里的逻辑是如果推送内容不为空就跳转到第二张页面

app的代码就完成了,现在就需要写自己实现apns的方法。java的实现代码

解压包后里面有一个com的文件夹和一个text的文件,把com文件夹放到java项目中,然后再写一个controller调用text文件里面的方法;

text里面加载了一个p12的文件,这个文件是我们申请的推送许可,生成方法

token:是我们app里面获取到的那个token,根据这个推送到设备上面。

如果想像极光推送那样发送附加字段可以在

String payload = APNS.newPayload().customField("type", 4).alertBody(content).badge(badge).build();
这句代码中加上.customField(),参数就是key-value的键值对,发生成功后在前台的tokenHandler里面直接result.key值就能获取到相应的value值