首页 > 学院 > 开发设计 > 正文

极光推送demo

2019-11-06 10:03:03
字体:
来源:转载
供稿:网友

代码—-测试版

发送端

使用rest api 接口:https://api.jpush.cn/v3/push

public class JPushUtil { public JPushUtil() { // TODO Auto-generated constructor stub } public static void main(String[] args) { String re = sendPost( "hello","100100");// 消息 // 则推送消息到app端 } /** * platform 必填 推送平台设置 * * audience 必填 推送设备指定 * * notification 可选 通知内容体。是被推送到客户端的内容。与message 一起二者必须有其一,可以二者并存 * * message 可选 消息内容体。是被推送到客户端的内容。与 notification 一起二者必须有其一,可以二者并存 * * sms_message 可选 短信渠道补 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,消息 和用户手机号码 * @return 所代表远程资源的响应结果 */ public static String sendPost(String news,String to) { String url="https://api.jpush.cn/v3/push"; PRintWriter out = null; BufferedReader in = null; String result = ""; try { //通知例子 JSONObject json = new JSONObject("{'platform': 'all','audience' : 'all'," + "'notification':{'android':{'alert' : '" + news+ "','extras':{'to':'1zhy'}}}, " + "'message': {" + "'msg_content': 'news'}}"); //通知例子 //json = new JSONObject("{'platform': 'all','audience' : 'all','notification':{'alert' : '"+news+"'}}"); //消息例子 json = new JSONObject("{'platform': 'all','audience' : 'all','message':{'msg_content' : '"+news+"','content_type':'text','title':'"+to+"'}}"); System.out.println(json.toString()); String secret = "密匙 key"; URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode(secret.getBytes())); conn.setRequestProperty("Content-Type", "application/json"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(json.toString()); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }}

接收端

使用sdk,加入相关包

public class myJpushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.e(TAG, "onReceive - " + intent.getAction()); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { Log.e(TAG, "JPush用户注册成功"); String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { //Action - JPushInterface.ACTION_MESSAGE_RECEIVED // JPushInterface.EXTRA_TITLE // 保存服务器推送下来的消息的标题 //JPushInterface.EXTRA_MESSAGE // 保存服务器推送下来的消息内容 //JPushInterface.EXTRA_EXTRA //保存服务器推送下来的附加字段。这是个 JSON 字符串 //JPushInterface.EXTRA_MSG_ID //SDK 1.6.1 以上版本支持。 //唯一标识消息的 ID, 可用于上报统计等 Log.e(TAG, "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // 自定义消息不会展示在通知栏,完全要开发者写代码去处理 // Push Talk messages are push down by custom message format processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { //Action - JPushInterface.ACTION_NOTIFICATION_RECEIVED. //JPushInterface.EXTRA_NOTIFICATION_TITLE //保存服务器推送下来的通知的标题。 //JPushInterface.EXTRA_ALERT // 保存服务器推送下来的通知内容JPushInterface.EXTRA_EXTRA // SDK 1.2.9 以上版本支持。 //保存服务器推送下来的附加字段。这是个 JSON 字符串 Log.d(TAG, "收到了通知"); System.out.println("EXTRA_INBOX收到了通知" + bundle.getString(JPushInterface.EXTRA_INBOX)); String ex = bundle.getString(JPushInterface.EXTRA_EXTRA); System.out.println("EXTRA_EXTRA收到了通知" + ex); if (ex != null && !ex.equals("{}")) { System.out.println("通知拦截 认证"); try { JSONObject json = new JSONObject(ex); String to = json.optString("to"); if (to.equals("zhy")) { System.out.println("通知拦截 认证 成功"); } else { System.out.println("通知拦截 认证 失败"); //abortBroadcast(); //Receiver1接收到广播后中断广播 } } catch (JSONException e) { e.printStackTrace(); } } else { System.out.println("通知不拦截"); } // 在这里可以做些统计,或者做些其他工作 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { //Action - JPushInterface.ACTION_NOTIFICATION_OPENED Log.d(TAG, "用户点击打开了通知"); System.out.println("用户点击打开了通知"); // 在这里可以自己写代码去定义用户点击后的行为 Intent i = new Intent(context, WebIndexActivity.class); //自定义打开的界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } else { Log.d(TAG, "Unhandled intent - " + intent.getAction()); } } private void processCustomMessage(Context context, Bundle bundle) { //title 发送目标 String title = bundle.getString(JPushInterface.EXTRA_TITLE); //message 发送内容 String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); System.out.println("title " + title); System.out.println("message " + message); showMessageNotification(context, title, message); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) void showMessageNotification(Context c, String who, String msg) { if (who.equals(BeeFrameworkApp.getInstance().getUser(c))) { System.out.println("消息拦截 认证 成功 发送通知 用户为"+BeeFrameworkApp.getInstance().getUser(c)); } else { System.out.println("消息拦截 认证 失败 不发送通知 用户为"+BeeFrameworkApp.getInstance().getUser(c)); return; } NotificationManager manager = (NotificationManager) c.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); Notification no = new Notification(); PendingIntent pend = PendingIntent.getActivity(c, 1, new Intent(c, WebIndexActivity.class), 0); //no.contentIntent=pend; // 1、创建一个自定义的消息布局 notification.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 //RemoteViews remoteView = new RemoteViews(c.getPackageName(), R.layout.notification); //remoteView.setImageViewResource(R.id.image, R.drawable.lbgs_icon); //remoteView.setTextViewText(R.id.text , "Hello,this message is in a custom expanded view"); //no.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) Notification.Builder builder = new Notification.Builder(c); // builder.setContentInfo("补充内容"); builder.setContentText(who + " " + msg); builder.setContentTitle("来宾国税排队通知"); builder.setSmallIcon(R.drawable.lbgs_icon); builder.setTicker("排队消息");//在通知栏上的内容的 builder.setAutoCancel(true); builder.setWhen(System.currentTimeMillis()); builder.setContentIntent(pend); builder.setDefaults(Notification.DEFAULT_ALL);// 设置使用默认声音、震动、闪光灯 no = builder.build(); try { manager.notify(10, no); } catch (Exception e) { System.out.println(e); } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表