起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5817|回复: 3

极光推送andriod简单分享

  [复制链接]

41

主题

209

帖子

418

积分

中级会员

Rank: 3Rank: 3

积分
418
QQ
发表于 2015-9-2 10:20:50 | 显示全部楼层 |阅读模式
前端js:
define(function(require){
        var $ = require("jquery");
        var justep = require("$UI/system/lib/justep");
          require("$UI/system/lib/cordova/cordova");
     require("cordova!cn.jpush.phonegap.JPushPlugin");

        var Model = function(){
                this.callParent();
        };

        Model.prototype.modelLoad = function(event){
                        var we=this;
                       
         
           var td={rows:[]};
           td.rows.push("tt");//设置tag名称
           var alias="bb";//设置alias
             var onDeviceReady   = function(){
                console.log("JPushPluginevice ready!")
                 window.plugins.jPushPlugin.setTagsWithAlias(td.rows,alias);
               // initiateUI();
               
            }
             var onGetRegistradionID = function(data) {
                try{
                    console.log("JPushPlugin:registrationID is "+data)
                  
                    //$("#registrationid").html(data);
                }
                catch(exception){
                    console.log(exception);
                }
            }
//               var onTagsWithAlias = function(event){
//                try{
//                    console.log("onTagsWithAlias");   
//                    var result="result code:"+event.resultCode+" ";
//                    result+="tags:"+event.tags+" ";
//                    result+="alias:"+event.alias+" ";
//                   we.comp("output1").set({value:result});
//                }
//                catch(exception){
//                    console.log(exception)
//                }
//            }
//             var onOpenNotification = function(event){
//                try{
//                    var alertContent
//                    if(device.platform == "Android"){
//                        alertContent=window.plugins.jPushPlugin.openNotification.alert;
//                    }else{
//                        alertContent= event.aps.alert;
//                    }
//                    //alert("open Notificaiton:"+alertContent);
//                    we.comp("output2").set({value:alertContent});
//                }
//                catch(exception){
//                    console.log("JPushPluginnOpenNotification"+exception);
//                }
//            }
//             var onReceiveNotification = function(event){
//                try{
//                    
//                    var eventContent="{";
//                    for(var key in event){
//                        if(key=="type"){
//                            break;
//                        }
//                       eventContent+=key+":"+JSON.stringify(event[key])+"\n"
//                    }
//                    eventContent+="}";
//                   we.comp("output2").set({value:eventContent});
//                    
//                }
//                catch(exeption){
//                    console.log(exception)
//                }
//            }
//             var onReceiveMessage = function(event){
//                try{
//                     var message = window.plugins.jPushPlugin.receiveMessage.message;
//                     //var extras = window.plugins.jPushPlugin.extras
//        
//                     we.comp("output3").set({value:message});
//                     
//                }
//                catch(exception){
//                    console.log("JPushPluginnReceiveMessage-->"+exception);
//                }
//            }
              var initiateUI = function(){
            
                    try{
                    window.plugins.jPushPlugin.init();
                   // window.plugins.jPushPlugin.resumePush();
                    window.plugins.jPushPlugin.getRegistrationID(onGetRegistradionID);
                    
                    if(device.platform == "Android"){
                        window.plugins.jPushPlugin.setDebugModeFromIos();
                        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
                    }else{
                        window.plugins.jPushPlugin.setDebugMode(true);
                    }
                    }
                    catch(exception){
                            console.log(exception);
                    }
                    }
                    //document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
            document.addEventListener("deviceready", onDeviceReady, false);
          // document.addEventListener("jpush.openNotification", onOpenNotification, false);
          // document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
          // document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
        };

        Model.prototype.button1Click = function(event){
//                                                var tag1 = this.comp("input1").val();
//                        var tag2 = this.comp("input2").val();
//                        var td={rows:[]};
//                        if(tag1 != ""){
//                                 td.rows.push(tag1);
//                            }
//                            if(tag2 != ""){
//                                td.rows.push(tag2);
//                            }
//                       var alias=this.comp("input3").val();
//                       
//                  window.plugins.jPushPlugin.setTagsWithAlias(td.rows,alias);  
//                                      
        };

        return Model;
});
后台代码将cn.jupsh.api下载下来拷贝下来复制到项目中,
package test;

import java.util.Collection;
import java.util.logging.Logger;

import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;



public class PushExample {
   // protected static final Logger LOG = (Logger) LoggerFactory.getLogger(PushExample.class);

  private static final String appKey ="c2941a39f055d49cac7758a5";//在极光官网上获得的appKey
   private static final String masterSecret = "b09751ca4e08bfc08aa5f475";//在极光官网上获得的masterSecret

   public static final String TITLE = "一帖一图";//通知内容
    public static final String ALERT = "hghgfhthrt";
    public static final String MSG_CONTENT = "Test from API Example - msgContent";
    public static final String REGISTRATION_ID = "1c9b974563af474b5dc18548";
    public static final String TAG = "tag_api";
   // static Collection<String>mm=new ;
   public static void main(String[] args) {
        testSendPush();
    }
   
   
    public static void testSendPush() {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
        
        // For push, all you need do is to build PushPayload object.
        PushPayload payload =buildPushObject_android_tag_alertWithTitle();

       /*

        * Collection<String> strings = new LinkedList<String>();

        * strings.add(REGISTRATION_ID);//配置多个客户端的极光注册id

        * PushPayload payload = buildPushObject_all_registrationids_alert(strings);

        */        

       try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
            
        } catch (APIConnectionException e) {
            System.out.println("Connection error. Should retry later. " + e);
            
        } catch (APIRequestException e) {
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }
   
    //发给一个客户端
    public static PushPayload buildPushObject_all_registrationid_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all()) //设置平台-所有平台
                .setAudience(Audience.registrationId(REGISTRATION_ID)) //设置受众-极光注册id
                .setNotification(Notification.alert(ALERT)) //设置通知 - 消息
                .build();
    }
   


   //多个客户端
    public static PushPayload buildPushObject_all_registrationids_alert(Collection<String> strings) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all()) //设置平台-所有平台
                .setAudience(Audience.registrationId(strings)) //设置受众-极光注册id-多个客户端
                .setNotification(Notification.alert(ALERT)) //设置通知-推送信息
                .build();
    }
    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("tt"))//发送给前面设置的tag
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }
}
写的不好请见谅,至于推送过去的url我还不知道怎么设置,只能取默认到首页,希望对你有所帮助

21

主题

62

帖子

134

积分

初级会员

Rank: 2

积分
134
QQ
发表于 2015-9-22 09:21:31 | 显示全部楼层
可以推送了,但怎么动态编写 推送的信息? 根据不同的用户登录,提示不同的信息? 就是点击提示信息直接进入相对应的页面?
回复 支持 反对

使用道具 举报

10

主题

50

帖子

122

积分

初级会员

Rank: 2

积分
122
QQ
发表于 2016-7-14 19:04:50 | 显示全部楼层
liangtao0510 发表于 2015-9-22 09:21
可以推送了,但怎么动态编写 推送的信息? 根据不同的用户登录,提示不同的信息? 就是点击提示信息直接进 ...

你好!请问你这个问题解决了吗
回复 支持 反对

使用道具 举报

15

主题

51

帖子

97

积分

初级会员

Rank: 2

积分
97
QQ
发表于 2016-12-28 10:45:45 | 显示全部楼层
请问上面的js 代码是写在index.js 中吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-3-28 23:34 , Processed in 0.073355 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表