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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools