• android126 zhihuibeijing 极光推送


    https://www.jpush.cn/

    张三把消息发送给自己的服务器,自己的服务器将消息发送给极光推送,然后极光推送将消息发送给妹子。

     

    清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itheima.pushdemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <permission
            android:name="com.itheima.pushdemo.permission.JPUSH_MESSAGE"
            android:protectionLevel="signature" />
    
        <!-- Required  一些系统要求的权限,如访问网络等 -->
        <uses-permission android:name="com.itheima.pushdemo.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:name=".MyApplication"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.itheima.pushdemo.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.itheima.pushdemo" />
                </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.itheima.pushdemo" />
                </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.itheima.pushdemo" />
                </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="1bf870ba4e7e4020d7943d33" /> <!-- </>值来自开发者平台取得的AppKey -->
        </application>
    
    </manifest>
    package com.itheima.pushdemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            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;
        }
    
    }
    package com.itheima.pushdemo;
    
    import android.app.Application;
    import cn.jpush.android.api.JPushInterface;
    
    /**
     * 自定义application,应用自己会有一个Application,这里继承了就用自己的Application不用系统的Application。
     * 这个Application是单例的。
     */
    public class MyApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            System.out.println("应用创建啦....");
            
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
        }
        
        public void doSomething() {
            System.out.println("do something...");
        }
    
    }
    package com.itheima.pushdemo;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import cn.jpush.android.api.JPushInterface;
    
    public class PushReceiver extends BroadcastReceiver {
    
        private static final String TAG = "PushReceiver";
    
        @Override
        public 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();
                }
    
            }
        }
    
    }
    ## 极光推送 ##
    
    所有需要客户端被动接收信息的功能模块,都可以用推送实现
    
    ## 推送原理 ##
    
    - xmpp 是一种基于TCP/IP的协议, 这种协议更适合消息发送.
    - socket 套接字, 发送和接收网络请求
    - 长连接 keep-alive, 服务器基于长连接找到设备,发送消息(客户端可以通过ip找到服务器,但是客户端的ip会变,所以要长连接保证服务器可以找到客户端)
    - 心跳包(客户端可能会有短暂的断网) , 客户端会定时(30秒一次)向服务器发送一段极短的数据(几个字节),作为心跳包, 服务器定时收到心跳,证明客户端活着,才会发消息.否则将消息保存起来,等客户端活了之后(重新连接),重新发送.

    > 客户端轮询(客户端定时主动拉取数据,客户端定时器去请求服务端接口), 浪费流量, 浪费性能
    > 谷歌推送服务(不能用,被墙了)

  • 相关阅读:
    数据库分库分表之后,你是如何解决事务问题?
    数据库周刊31丨openGauss 正式开源;7月数据库排行榜发布;浙江移动国产数据库AntDB迁移;oracle ADG跨版本搭建;PG解决社保问题;mysqlbinlog解析……
    2020年7月国产数据库排行:华为、腾讯发新品,中兴、阿里结硕果
    47%的MongoDB数据库遭黑客比特币勒索,你中招了吗?中招怎么办?
    数据库周刊30丨数据安全法草案将亮相;2020数据库产业报告;云南电网上线达梦;达梦7误删Redo Log;Oracle存储过程性能瓶颈;易鲸捷实践案例……
    数据库周刊29│2020数据库研究报告;Oracle取消今年技术大会;腾讯云DBbridge发布支持一键迁库;饿了么迁至阿里云;PG数组查询;Oracle被比特币勒索;DM8 安全管理…
    数据库周刊28│开发者最喜爱的数据库是什么?阿里云脱口秀聊程序员转型;MySQL update误操作;PG流复制踩坑;PG异机归档;MySQL架构选型;Oracle技能表;Oracle文件损坏处理……
    怎么查看HBase表的创建时间
    Hadoop/HBase Kerberos认证失败:Clock skew too great
    java线程莫名异常退出时,如何捕获异常信息
  • 原文地址:https://www.cnblogs.com/yaowen/p/5074722.html
Copyright © 2020-2023  润新知