179.n1-极光推送
来源:互联网 发布:北京永乐中汉网络 编辑:程序博客网 时间:2024/04/29 12:47
官网地址https://www.jiguang.cn/push
## 推送原理 ##
- xmpp 是一种基于TCP/IP的协议, 这种协议更适合消息发送
- socket 套接字, 发送和接收网络请求
- 长连接 keep-alive, 服务器基于长连接找到设备,发送消息
- 心跳包 , 客户端会定时(30秒一次)向服务器发送一段极短的数据,作为心跳包, 服务器定时收到心跳,证明客户端或者,才会发消息.否则将消息保存起来,等客户端活了之后(重新连接),重新发送.
> 客户端轮询(客户端定时主动拉取数据), 浪费流量, 浪费性能
Mainifis文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ldw.push" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <permission android:name="com.ldw.push.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required 一些系统要求的权限,如访问网络等 --> <uses-permission android:name="com.ldw.push.permission.JPUSH_MESSAGE" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- Optional for location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="com.ldw.push.MyApplication" android:theme="@style/AppTheme" > <activity android:name="com.ldw.push.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Required SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Translucent.NoTitleBar" > <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.ldw.push" /> </intent-filter> </activity> <!-- Required SDK核心功能 --> <service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" > </service> <!-- Required SDK 核心功能 --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" > <intent-filter android:priority="1000" > <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!-- Required 显示通知栏 --> <category android:name="com.ldw.push" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <!-- Optional --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" /> <!-- User defined. 用户自定义的广播接收器 --> <receiver android:name=".PushReceiver" android:enabled="true" > <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用户注册SDK的intent --> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用户接收SDK消息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用户接收SDK通知栏信息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用户打开自定义通知栏的intent --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 --> <category android:name="com.ldw.push" /> </intent-filter> </receiver> <!-- Required . Enable it you can get statistics data with channel --> <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default" /> <meta-data android:name="JPUSH_APPKEY" android:value="bca36288203977d0722a7996" /> <!-- </>值来自开发者平台取得的AppKey --> </application></manifest>
MainActivity.java
package com.ldw.push;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //使用自定义的application MyApplication application = (MyApplication) getApplication();application.doSomething(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
自定义Application来取代系统Application MyApplication.java
package com.ldw.push;import android.app.Application;import cn.jpush.android.api.JPushInterface;/* * 自定义application */public class MyApplication extends Application{@Overridepublic void onCreate() {super.onCreate();System.out.println("应用创建啦....");JPushInterface.setDebugMode(true); JPushInterface.init(this);}public void doSomething() {System.out.println("do something...");}}
自定义广播接收者PushReceiver.java
package com.ldw.push;import org.json.JSONException;import org.json.JSONObject;import cn.jpush.android.api.JPushInterface;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.util.Log;public class PushReceiver extends BroadcastReceiver{private static final String TAG = "PushReceiver";@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();Log.d(TAG, "onReceive - " + intent.getAction());if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {System.out.println("收到了自定义消息。消息内容是:"+ bundle.getString(JPushInterface.EXTRA_MESSAGE));// 自定义消息不会展示在通知栏,完全要开发者写代码去处理} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {System.out.println("收到了通知");// 在这里可以做些统计,或者做些其他工作} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {System.out.println("用户点击打开了通知");// 在这里可以自己写代码去定义用户点击后的行为String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);System.out.println("附加信息:" + extra);try {JSONObject jo = new JSONObject(extra);String url = jo.getString("url");System.out.println("url:" + url);// 跳浏览器加载网页} catch (JSONException e) {e.printStackTrace();}}}}
阅读全文
0 0
- 179.n1-极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 推送 、极光
- 极光推送
- 极光推送
- 特征选择mRMR算法代码实现及安装下载
- Java:String、StringBuffer和StringBuilder的区别
- unity shader _MainTex_TexelSize 和 UNITY_UV_STARTS_AT_TOP 的作用
- java 数组初始化注意事项
- spring boot 、 spring cloud 中使用servlet形式get、post方式http请求例子,并且和原生servlet有区别
- 179.n1-极光推送
- 浅谈引用——让初学者少走弯路
- ibaties保存不报错,数据进不到数据库
- 数据库设计与性能优化(一)
- 飞机大战项目(1)主菜单
- 最短路 dijkstra
- 静态语言与动态语言
- 安装androidstudio出现 Unable to access Android SDK add-on list
- Angular 2 CLI 开发环境的建立与App应用开发入门