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

概述

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

分析例程源码

    首先分析一下例程的源码结构,建议对照着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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 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: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

See all articles

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools