Android集成极光推送服务

来源:互联网 发布:js购物车功能怎么实现 编辑:程序博客网 时间:2024/05/07 20:41

手机推送服务对于现在的应用可以说是很普遍,今天我们就来集成实现极光推送服务 
要想实现极光推送我们需要到极光推送官网注册账号成为开发者然后申请获取Key 注册地址为:https://www.jiguang.cn/ 注册完成后进入控制台然后创建应用 如图:应用名称可以随便填写 应用包名是你创建项目时的主包名可不要填错了 
这里写图片描述 
创建完成后就会看到我们获取的Key 
这里写图片描述 
接下来我们去下载需要的SDK 地址:http://docs.jiguang.cn/jpush/resources/ 选择Android SDK下载 
这里写图片描述 
下面我们新建一个工程项目 名为JPushDemo 注意创建项目时要保证和你在极光推送平台上的应用包名一致 然后我们把刚才下载的SDK解压 把libs中的 
这里写图片描述 
接着在main下面创建一个文件夹名为jniLibs把SDKlibs中的so文件拷贝进去 
这里写图片描述 
项目的结构如下: 
这里写图片描述 
接下来我们就根据官方文档配置我们的项目吧 在AndroidManifest中 添加代码如下

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.shiran.jpushdemo">    <!-- Required -->    <!-- 注意权限处有些地方也需要在前面加上包名如下 -->    <permission        android:name="com.shiran.jpushdemo.permission.JPUSH_MESSAGE"        android:protectionLevel="signature" />    <!-- Required -->    <uses-permission android:name="com.shiran.jpushdemo.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.WRITE_SETTINGS" />    <!-- Optional. Required for location feature -->    <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="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!-- Required SDK 核心功能 -->        <!-- option since 2.0.5 可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process参数 将JPush相关组件设置为一个独立进程 -->        <!-- 如:android:process=":remote" -->        <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>        <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->        <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->        <service            android:name="cn.jpush.android.service.DaemonService"            android:enabled="true"            android:exported="true">            <intent-filter>                <action android:name="cn.jpush.android.intent.DaemonService" />                <category android:name="com.shiran.jpushdemo" /> <!-- 你自己的包名 -->            </intent-filter>        </service>        <!-- Required -->        <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" />                <category android:name="com.shiran.jpushdemo" /> <!-- 你自己的包名 -->            </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核心功能 -->        <activity            android:name="cn.jpush.android.ui.PushActivity"            android:configChanges="orientation|keyboardHidden"            android:exported="false">            <intent-filter>                <action android:name="cn.jpush.android.ui.PushActivity" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="com.shiran.jpushdemo" /> <!-- 你自己的包名 -->            </intent-filter>        </activity>        <!-- Required SDK核心功能 -->        <service            android:name="cn.jpush.android.service.DownloadService"            android:enabled="true"            android:exported="false"></service>        <!-- Required SDK核心功能 -->        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />        <!-- User defined. 用户自定义的广播接收器 -->        <receiver            android:name="com.shiran.jpushdemo.MyReceiver"            android:enabled="true">            <intent-filter>                <!-- Required 用户注册SDK的intent -->                <action android:name="cn.jpush.android.intent.REGISTRATION" />                <!-- Required 用户接收SDK消息的intent -->                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />                <!-- Required 用户接收SDK通知栏信息的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />                <!-- Required 用户打开自定义通知栏的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />                <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />                <!-- 接收网络变化 连接/断开 since 1.6.3 -->                <action android:name="cn.jpush.android.intent.CONNECTION" />                <category android:name="com.shiran.jpushdemo" /> <!-- 你自己的包名 -->            </intent-filter>        </receiver>        <!-- Required. For publish channel feature -->        <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。 -->        <!-- 例如: -->        <!-- 发到 Google Play 的APK可以设置为 google-play; -->        <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->        <!-- 目前这个渠道统计功能的报表还未开放。 -->        <meta-data            android:name="JPUSH_CHANNEL"            android:value="developer-default" />        <!-- Required. AppKey copied from Portal -->        <meta-data            android:name="JPUSH_APPKEY"            android:value="5398520065ca5e238abda9b0" /><!-- 获取的Key -->        <activity android:name=".HelloActivity"></activity>    </application></manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149

清单文件中配置完成后在我们的MainActivity中加上两行代码就可以实现我们的极光推送啦

package com.shiran.jpushdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import cn.jpush.android.api.JPushInterface;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        JPushInterface.setDebugMode(true);//设置为调试模式 正式环境下改为false        JPushInterface.init(this);//初始化    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.shiran.jpushdemo.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="我们是来使用极光推送服务哒!"        android:textSize="16sp"/></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

大功告成可以运行测试下啦 在我们的极光推送控制台发送一条消息试试吧 
这里写图片描述 
这里写图片描述 
这是测试收到的推送消息 发现我模拟器的时间不同啊 不过也没事 推送能使用就好!!!~ 
这里写图片描述 
其实到这里集成的就差不多了,但还可以对发来的消息做处理 我就顺便说下吧 
极光推送中有一个自定义消息 这个自定义消息需要我们定义一个广播接收器 文档中提供的有就拿来直接用吧 
创建一个自定义接收器名为MyReceiver 代码如下

package com.shiran.jpushdemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.widget.Toast;import cn.jpush.android.api.JPushInterface;/** * 自定义接收器 */public class MyReceiver extends BroadcastReceiver {    private static final String TAG ="JPush";    @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())) {            Toast.makeText(context, "点开了通知", Toast.LENGTH_SHORT).show();            // 在这里可以自己写代码去定义用户点击后的行为            Intent i = new Intent(context, HelloActivity.class);  //自定义打开的界面 用户收到极光推送的信息后然后点击进入到的页面            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            context.startActivity(i);        } else {            Log.d(TAG, "Unhandled intent - " + intent.getAction());        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

我们可以对收到的广播做相应的处理 这里我让收到推送消息时然后点击进入到HelloActivity中 
这里写图片描述 
HelloActivity中代码和布局 也顺便贴出来吧~~~

package com.shiran.jpushdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class HelloActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_hello);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_hello"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.shiran.jpushdemo.HelloActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="极光推送的消息"        android:textSize="18sp"/></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
0 0
原创粉丝点击