一、首先做一些简单的操作
打开百度首页,然后搜索 --- 极光推送
然后选择打开极光推送的官方网站
打开后,先进行相关的用户名的注册
然后登录进去
在这里面,可以点击文档,然后下载开发所用的开发文档与sdk,或者开发教程视频
然后选择控制台,在控制台中创建一个新的应用
注意,这里添加的应用包名应与所要开发推送功能的应用的包名一致
创建完成后,会生成一个AppKey
二、安卓项目中
首先是将jar包与.os文件引入项目中
然后 在Application中进行相关配制
package com.administrator.pullrefresh;
import android.app.Application;
import cn.jpush.android.api.JPushInterface;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
然后注册一个广播用来监听接收到的推送消息
package com.administrator.pullrefresh;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONObject;
import cn.jpush.android.api.JPushInterface;
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Bundle bundle = intent.getExtras();
// Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Log.d(TAG, "JPush用户注册成功");
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的自定义消息");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的通知");
receivingNotification(context,bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");
openNotification(context,bundle);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void receivingNotification(Context context, Bundle bundle){
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
Log.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
Log.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.d(TAG, "extras : " + extras);
}
private void openNotification(Context context, Bundle bundle){
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String myValue = "";
try {
//当前的url的值是由,极光推送的服务传递过来的,http://www.itheima.com
// {
// url:"http://www.itheima.com";
// }
JSONObject extrasJson = new JSONObject(extras);
myValue = extrasJson.optString("url");
} catch (Exception e) {
Log.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
/**
* 接收到消息后要点击要跳转的页面
* 这里是跳转到另一个Activity进行页面信息的显示
*/
Intent mIntent = new Intent(context, ThisActivity.class);
mIntent.putExtra("url", myValue);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
那么在信息显示页面中
package com.administrator.pullrefresh;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class ThisActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thisactivity);
//http://www.ooxx.com/abcd.html--->WebView
/**
* 这里接收信息
* 将接收到的信息进行简单的一个显示操作
*/
String url = getIntent().getStringExtra("url");
Toast.makeText(getApplicationContext(), url, 1).show();
}
}
接下来就是配制最重要的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.administrator.pullrefresh">
<!-- 这里的包名要与极光推送页面中创建应用的包名一至-->
<!-- Required -->
<permission android:name="com.administrator.pullrefresh.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />
<!-- Required -->
<uses-permission android:name="com.administrator.pullrefresh.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.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!--since 1.6.0 -->
<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<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:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name="com.administrator.pullrefresh.MyApplication">
<activity
android:name="com.administrator.pullrefresh.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.administrator.pullrefresh.ThisActivity"
android:label="@string/app_name">
</activity>
Required
<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>
<!-- Required -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true" >
<intent-filter android:priority="1000"> <!--since 1.3.5 -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--since 1.3.5 -->
<category android:name="com.administrator.pullrefresh" /> <!--since 1.3.5 -->
</intent-filter> <!--since 1.3.5 -->
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<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>
<receiver
android:name="com.administrator.pullrefresh.MyBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<category android:name="com.administrator.pullrefresh" />
</intent-filter>
</receiver>
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.administrator.pullrefresh" />
</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" />
<!-- 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="561ec8ebeb6e85d9e2946f53"/>
</application>
</manifest>
然后就可以将项目发布到手机上进行相关测试
三、发送消息推送,进行相关测试
测试Demo下载地址