关于极光推送的使用步骤
自己使用过程中的配置步骤
1.去极光官网注册一个账号,并且新建一个项目网上一大堆不详细介绍了。注意包名。然后下载android的例子工程
2.自己在本地创建一个和刚才一样包名的工程。
3.copy相应的代码和资料。包括 manifest.xml的一些权限。剔除一些自己不需要的然后复制进来即可。
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.xunmeng.jpush.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!-- Required 一些系统要求的权限,如访问网络等-->
<uses-permission android:name="com.xunmeng.jpush.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.WRITE_SETTINGS" />
<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.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- Optional for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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" />
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.xunmeng.jpush" />
</intent-filter>
</activity>
<!-- Required SDK核心功能-->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false"></service>
<!-- Required SDK 核心功能-->
<!-- 可配置android:process参数将PushService放在其他进程中 -->
<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 SDK核心功能-->
<receiver
android:name="cn.jpush.android.service.PushReceiver" />//换成自己的广播接收器
android:enabled="true"
android:exported="false">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<!--Required 显示通知栏 -->
<category android:name="com.xunmeng.jpush" />
</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核心功能-->
<receiver
android:name="cn.jpush.android.service.AlarmReceiver"
android:exported="false" />
<!-- User defined. For test only 用户自定义的广播接收器-->
<receiver
android:name=".receiver.MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<!--Required 用户注册SDK的intent-->
<action android:name="cn.jpush.android.intent.UNREGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<!--Required 用户接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<!--Required 用户接收SDK通知栏信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<!--Required 用户打开自定义通知栏的intent-->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />
<!--Optional 用户接受Rich Push Javascript 回调函数的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION" />
<!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="com.xunmeng.jpush" />
</intent-filter>
</receiver>
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="3c00c4abcf5fea7350d2f433" />
<!-- </>值来自开发者平台取得的AppKey-->
4.复制相应的例子工程lib文件进入自己的lib中。(jinLibs和jpush-android-*******)等
android studio注意在build.gradle中加入NDk的引用
5.在Application执行
JPushInterface.init(context.getApplicationContext());
初始化即可,我自己封装的工具类。
把使用的逻辑和代码封装在里面。
6.自定义广播接收器
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JPushReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
Log.d("MyReceiver", "[PushReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//打开通知栏
showNotification(context, bundle);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
}
else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
/**
* 通知
*
* @param context
* @param bundle
*/
public void showNotification(Context context, Bundle bundle) {
try {
String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
LogUtils.d(extra);
NotifyEntity notifyEntity = new Gson().fromJson(extra, NotifyEntity.class);
LogUtils.d("notifyEntity = " + notifyEntity);
int type = notifyEntity.getType();
String title = notifyEntity.getTitle();
String content = notifyEntity.getContent();
String message = notifyEntity.getMessage();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, MainActivity.class);
if (1 == type) {
// intent.putExtra(IntentKeyConstant.activity_url, content);
}
Log.e("showNotification",title);
Log.e("showNotification",content);
Log.e("showNotification",message);
Log.e("showNotification",""+type);
PendingIntent pendingIntent = PendingIntent.getActivity(context, Jpush.code, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {//兼容 4.0
Notification.Builder builder = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis());
notification = builder.getNotification();
} else {//兼容4.1及以上
notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.build();
}
notification.defaults = Notification.DEFAULT_ALL;
manager.notify(Jpush.code + 1, notification);
Jpush.code++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
即可实现推送。
自己写的不是很全面 下面可以看看大神怎么使用的。查缺补漏,欢迎提意见。
关于推送服务,国内有很多选择,笔者也对它们进行了一个详细的对比,一般我们产品选择推送服务主要考量以下几个要素:
1、是否收费,如何收费?
2、推送内容是是什么(是否包含通知、消息、富媒体等等)
3、稳定性、及时性如何?
4、集成难度是否简单
5、支持平台有哪些(主流Android、IOS)
6、服务端支持语言(Java、C#、PHP、Python等)
下面笔者例举国内主要的一些推送服务:
来自Devstore的统计,共收录了国内21家推送服务,分别是(按关注度排列):
1.个推(个信互动(北京)网络科技有限公司http://www.igetui.com/)
2.百度云推送(百度http://developer.baidu.com/cloud/push)
3.极光推送(深圳市和讯华谷信息技术有限公司https://www.jpush.cn/)
4.友盟推送(友盟http://www.umeng.com/push)
5.小米推送(小米http://dev.xiaomi.com/doc/?page_id=1670)
6.腾讯信鸽推送(腾讯公司http://xg.qq.com/)
7.Bmob推送(广州市比目网络科技有限公司http://www.bmob.cn/)
8.云巴推送(深圳市微智云科技有限公司http://www.yunba.io/)
9.华为推送(华为公司http://developer.huawei.com/push)
10.智游推送(北京智游网安科技有限公司http://www.zypush.com/)
11.盛大云推送(盛大网络http://www.grandcloud.cn/product/push)
12.原子推送(原子技术有限公司http://www.atom14.com/)
13.魔桥推送(魔桥http://www.mobbridge.com/)
14.魔泊网推送(魔泊网http://helpdocs.sturgeon.mopaas.com/helpdocs/_push.html)
15.有推推送(中国移动通信http://dev.10086.cn/aoi/index.jsp)
16.WeCloud(WeCloud http://www.wecloud.io/)
17.Learn Cloud推送(美味书签信息技术有限公司https://cn.avoscloud.com/)
18.亚马逊推送(亚马逊公司http://aws.amazon.com/cn/sns/)
19.魔方推送(魔方公司http://www.imofan.com/)
20.语盒团推送(语盒团公司http://www.yuchteam.com/)
21.移动消息推送
开发者可以针对产品的需求,来对比选择适合自己应用的推送服务。
笔者这里选择了“极光推送”,它是部分收费的,收费模式各位可以到官网查看;支持推送的内容有通知、消息、富媒体,稳定性好、能及时到达、提供服务API、支持Android、iOS平台,服务端支持Java、PHP、Python、C#、Ruby、Node.js。
集成极光推送笔者这里也不详细写,主要提几点:
1、使用Portal来进行测试
Portal是服务提供的传送门,我们可以使用控制台来进行推送测试,实际应用时一般是根据推送服务提供的服务端API来实现定制推送。
极光Portal如下:
2、通知与消息的区别
通知就是可以再通知栏显示提醒用户的信息,而消息不会在通知栏显示,业务逻辑可以完全有开发者来定。
3、推送对象
可以分为:
广播:会把通知无区别的推送到每个人身上。
设置标签:这一般用于群组推送。
设置别名:适用于单播,根据客户端设置的别名来推送。
设置注册ID:适用于单播推送,指定推送给某一个人,可以使注册过的用户ID,主要用来区分。
4、自定义通知栏样式、附加字段的作用
我们有时候可能不想直接用Android原生的通知栏样式,如果服务提供相应的API的话,我们可以通过自定义布局来达到这个目的。极光这里提供了以下方法:
- // 自定义Notification样式
- CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
- getApplicationContext(),
- R.layout.customer_notitfication_layout, R.id.icon, R.id.title,
- R.id.text);
- builder.layoutIconDrawable = R.drawable.ic_launcher;
- builder.developerArg0 = "developerArg2";
- JPushInterface.setPushNotificationBuilder(2, builder);
- Toast.makeText(getApplicationContext(), "Custom Builder - 2",
- Toast.LENGTH_SHORT).show();
我们只需要指定通知栏编号,下次推送通知的时候就会以自定义的通知栏样式来显示。
这里还有一个附加字段,我们有时候可能需要根据推送的不同消息来实现跳转不同的页面,这时候就可能需要用到附加字段了,我们在Broadcast Receiver来接受推送下来的消息,解析附加字段内容,来达到我们的目的。
代码示例:
- package com.infzm.daily.know.receiver;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import cn.jpush.android.api.JPushInterface;
- import com.infzm.daily.know.ArticleDetailActivity;
- import com.infzm.daily.know.MainActivity;
- import com.infzm.daily.know.utils.LogUtils;
- public class PushReceiver extends BroadcastReceiver {
- private static final String TAG = "JPush";
- @Override
- public void onReceive(Context context, Intent intent) {
- Bundle bundle = intent.getExtras();
- LogUtils.logi(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: "+ printBundle(bundle));
- if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
- String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
- LogUtils.logi(TAG, "[PushReceiver] 接收Registeration Id : " + regId);
- } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
- LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
- }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
- LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知");
- int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
- LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
- } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
- LogUtils.logi(TAG, "[PushReceiver] 用户点击打开了通知");
- String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
- LogUtils.loge(TAG, "type:" + type);
- try {
- JSONObject jsonObject = new JSONObject(type);
- String str = jsonObject.getString("key");
- if (str.equals("1")) {
- //打开自定义的Activity
- Intent i = new Intent(context, MainActivity.class);
- bundle.putInt("index", 1);
- i.putExtras(bundle);
- //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
- context.startActivity(i);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
- LogUtils.logi(TAG, "[PushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
- //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
- } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
- boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
- LogUtils.logi(TAG, "[PushReceiver]" + intent.getAction() +" connected state change to "+connected);
- } else {
- LogUtils.logi(TAG, "[PushReceiver] Unhandled intent - " + intent.getAction());
- }
- }
- // 打印所有的 intent extra 数据
- private static String printBundle(Bundle bundle) {
- StringBuilder sb = new StringBuilder();
- for (String key : bundle.keySet()) {
- if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
- sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
- }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
- sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
- }
- else {
- sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
- }
- }
- return sb.toString();
- }
- }