Home Backend Development PHP Tutorial Android客户端与PHP服务端通信(四)-极光推送示例工程分析

Android客户端与PHP服务端通信(四)-极光推送示例工程分析

Jun 23, 2016 pm 01:35 PM

概述

    上一节,描述了注册极光推送并使用其例子的方法,这一节准备研究一下示例工程的框架,为移植它做准备。

分析例程源码

    首先分析一下例程的源码结构,建议对照着JPUSH的官方文档(http://docs.jpush.io/)分析,我就是这样做的。

    注册应用后,下载的示例工程结构如下,



    ExampleApplication.java:该类为应用程序定制了一个Application类,因为调用JPush的SDK时,需要调用JPush提供的init()函数API,而按照官方文档的说明“init 只需要在应用程序启动时调用一次该API即可”,故而定制了一个Application类,如果你在MainActivity中调用init(),可能会造成init()的多次调用。当然,定义的该ExampleApplication类需要在 AndoridManifest.xml 里配置。

    ExampleUtil.java:顾名思义,“Util”表示的就是一些全局的方法或者属性,在这个类里定义了一些静态函数,比如获取IME码、获取APPKEY等。

    MainActivity.java:这个都知道,就是应用的主界面了。

    MyReceiver.java:这个继承了BroadcastReceiver,是一个自定义接收器继承类。如果不定义这个类,用户点击推送通知后默认跳转到主界面,也接收不到自定义消息。这个类需要同样需要在AndoridManifest.xml 里配置,如下图。



    PushSetActivity.java:这个类主要对JPush进行“高级选项”设置,比如设置Tag和Alias、定制通知栏样式。

    SettingActivity.java:这个类也是对JPush进行设置,主要是设置接收push时间。

    TestActivity.java:这个类继承Activity,主要是用来显示用户接收到的推送消息,当用户点击通知栏的推送消息后,会跳转到这个Activity。


    下面再来分析一下AndoridManifest.xml,下图为内容完整截图


2行:指定Android的命名空间

3行:指定标准的应用包名,也是一个应用程序的默认名称。这里的包名可能会有人将其与src文件夹下的com.example.jpushdemo包比较从而混淆。其实专业开发android的比我知道,一个src可以包含多个自定义包,这个com.example.jpush包只是对包含了对JPush的一些调用操作,日后你如果对JPush比较熟悉了,完全可以将对JPush的操作进行抽离,封装在一个自定义的包里。

4行:设备应用程序版本识别码,必须是一个整数值,代表app更新过多少次,我们修改到自己的时候可以自定义。

5行:为用户查看版本用,需要具备一定的可读性。

7行:指定应用程序中需要使用的SDK版本,比如最低版本,最高版本和目标版本。

8-10行:自定义用户权限。注意其中的android:name="com.lygk.lovelife.permission.JPUSH_MESSAGE",如果你的操作JPush的包名是com.test.jpushdemo ,那么这里就是android:name="com.test.jpushdemo.JPUSH_MESSAGE",其实这样说也不是对的,只是一个命名而已,只要保证在其他引用这个权限的地方使用的名字和这个一致就可以了,比如13行就声明了这个自定义权限。

13-33行:应用程序所需要的权限的声明,这些可以从官方文档中查找到。

34-139行:应用程序的配置根元素,包含所有与应用有关配置的元素。

35行:应用图标

36行:应用名

37行:这个就是咱们自己定义的Applicatioin类的名字,也就是应用启动的是com.example.jpushdemo包下的ExampleApplication。

40-47行:Activity活动组件(即界面控制器组件)声明,对应MainActivity.java。注意Android应用中的每一个Activity都必须在AndoridManifest.xml配置文件中声明,否则系统不识别也不执行该Activity。

49行:Activity活动组件声明,对应PushSetActivity.java

51行:Activity活动组件声明,对应SettingActivity.java

53-58行:Activity活动组件声明,对应TestActivity.java

62-71行:Activity活动组件声明,对应JPush SDK包中定义的PushActivity.java,我充分怀疑这个就是手机接收到推送消息时显示在通知栏的那个Activity。

73-77行:Service服务组件的声明标签,用于定义与描述一个具体的Android服务。其中android:name表示Service服务类名,android:enabled表示服务开关,android:exported指示该服务是否能够被其他应用程序组件调用或跟它交互。

81-92行:JPush SDK包定义的PushService服务的声明。

95-113行:Boardcast Receiver广播接收器组件的声明,用于定义与描述一个具体的Android广播接收器。android:name表示Boardcast Receiver接收器类名,android:enabled表示接收器开关。注意其中的用于Intent消息过滤器的声明。中必须包含有元素,即用于描述具体消息的名称;标签则用于表示能处理消息组件的类别,即该Action所符合的类别。

115行:Boardcast Receiver广播接收器组件的声明,这里是AlarmReceiver。

119-132行:Boardcast Receiver广播接收器组件的声明,这里是MyReceiver自定义广播接收器,对应的是源码包的MyReceiver.java。

136行:标签JPUSH_CHANNEL声明,用于存储预定义数据,和类似,也可以放在这四个元素标签中。Meta数据一般会以键值对的形式出现,个数没有限制,而这些数据都将被放到一个Bundle对象中,程序中我们则可以使用ActivityInfo、ServiceInfo甚至ApplicationInfo对象的metaData属性中读取。

137行:标签JPUSH_APPKEY声明,这个是你在极光推送官网创建应用时系统分配的AppKey,如下图红线框出所示。



结尾

    上面对在极光推送官网上创建应用时自动生成的示例代码的框架进行了分析,经过这么分析,我总体上知道该怎么去移植到自己的应用中了,下一节我准备修改移植到自己的Demo程序上。

/****************************************************************************************************

*鲁阳高科工作室

*网       址:www.bigbearking.com

*商务合作QQ:1519190237

*业 务 范 围:网站建设、桌面软件开发、Android\IOS开发、图像影视后期处理、PCB设计

****************************************************************************************************/
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles