您的位置:首页 > 编程语言

在ionic项目中使用极光推送实现推送 & 服务器端代码

2015-08-25 14:17 561 查看
ionic start -a jPushDemo -i com.lawxin.fengkong jpushdemo blank

meteor add cordova:cn.jpush.phonegap.JPushPlugin@https://github.com/jpush/jpush-phonegap-plugin/tarball/f54252dd4f28874319c002cf0d066b6adb865c1f

http://ionichina.com/topic/54f96e7b59a9bdef119234a1

下载之后,手动修改以下文件,也可以实现。需要注意的是极光有延迟,稍后便能看到效果。

cn.jpush.phonegap.JPushPlugin.java
src/android/AndroidManifest.xml
src/ios/PushConfig.plist

source code:http://vdisk.weibo.com/s/ao-ZYIoZdUTFI

取设备ID

//在非登陆页使用
document.addEventListener("deviceready", function () {
//alert(10000)
if (undefined != window.plugins
&& undefined != window.plugins.jPushPlugin){
//alert("on phone");
//启动极光推送服务
window.plugins.jPushPlugin.init();
window.plugins.jPushPlugin.getRegistrationID(function (result) {

if (result){
var registrationID = result;
console.log("registrationID",registrationID);
//alert("init registrationID"+result);
//alert("init registrationID"+result);
}
//alert("init registrationID"+result);
console.log("result registrationID",registrationID);
});
}
}, false);


  

server code:

package main

import (
"fmt"
"github.com/ylywyn/jpush-api-go-client"
)

const (
appKey = ""
secret = ""
)

func main() {

//Platform
var pf jpushclient.Platform
pf.Add(jpushclient.ANDROID)
pf.Add(jpushclient.IOS)
//pf.Add(jpushclient.WINPHONE)
//pf.All()

//Audience
var ad jpushclient.Audience
s := []string{"011364bb516","0000103808e"}
//ad.SetTag(s)
//ad.SetAlias(s)
ad.SetID(s)
//ad.All()

//Notice
message := "您有1条消息风控信息,请注意查收"
var notice jpushclient.Notice
notice.SetAlert(message)
notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: message})
notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: message})
//notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})

var msg jpushclient.Message
msg.Title = "风控宝"
msg.Content = message

payload := jpushclient.NewPushPayLoad()
payload.SetPlatform(&pf)
payload.SetAudience(&ad)
payload.SetMessage(&msg)
payload.SetNotice(¬ice)

bytes, _ := payload.ToBytes()
fmt.Printf("%s\r\n", string(bytes))

//push
c := jpushclient.NewPushClient(secret, appKey)
str, err := c.Send(bytes)
if err != nil {
fmt.Printf("err:%s", err.Error())
} else {
fmt.Printf("ok:%s", str)
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: