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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment