在ionic專案中使用極光推送實現推送 & 伺服器端程式碼

石橋碼農發表於2015-08-25

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(&notice)

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

  

 

相關文章