如何快速的实现极光推送
来源:互联网 发布:ubuntu 配置openstack 编辑:程序博客网 时间:2024/05/22 12:40
因为自己已经没有用EC(eclipse)开发(因为已经停止维护了),现在基本都是用的AS(AndroidStudio),推荐使用AS,以下的都是以AS开发工具为粒子的
我们使用极光最新版的,最新版的我们在gradle里面吧下面的几个配置好,然后再Manifest就不需要再配置了(之后只需要吧你自己MyRecever配置一下就OK了)(这个是我最喜欢的,之前都需要配置一大板)
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
1.在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush上注册的包名.
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
......
}
......
}
dependencies {
......
compile 'cn.jiguang.sdk:jpush:3.0.9' // 此处以JPush 3.0.9 版本为例。
compile 'cn.jiguang.sdk:jcore:1.1.7' // 此处以JCore 1.1.7 版本为例。
......
}
2.以下是极光推送的权限配置
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" /><!-- Required --><permission android:name="您应用的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /><!-- Required --><uses-permission android:name="您应用的包名.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" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!-- Optional. Required for location feature --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 --><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" /><uses-permission android:name="android.permission.GET_TASKS" />
3.配置自定义的广播类
<!-- 极光推送 用户自定 --><!-- User defined. 用户自定义的广播接收器 --><receiver android:name=".jpush.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" /> <!-- 接收网络变化 连接/断开 since 1.6.3 --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="${applicationId}" /> </intent-filter></receiver>
4.最后不要忘了在自定义的Application里面初始化 ()
<application android:name=".base.BaseApplication" android:allowBackup="true" android:icon="@drawable/logo"
比如我的是BaseApplication,这个是自己写的类继承Application这个类的
@Overridepublic void onCreate() { super.onCreate(); /** * 极光推送 初始化 * */ JPushInterface.setDebugMode(true); JPushInterface.init(this);
里面自定义的Receiver我也贴一下我自己的吧,大家可以根据自己项目的需要进行处理
package com.example.ruidun.ShanXunApplication.jpush;import android.app.ActivityManager;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import com.example.ruidun.ShanXunApplication.ui.activity.HomeDetailActivity;import com.example.ruidun.ShanXunApplication.ui.activity.NewsDetailActivity;import com.example.ruidun.ShanXunApplication.ui.activity.SetsActivity;import com.example.ruidun.ShanXunApplication.ui.activity.WebActivity;import com.example.ruidun.ShanXunApplication.utils.ConstantTools;import com.example.ruidun.ShanXunApplication.utils.GlobalTools;import com.example.ruidun.ShanXunApplication.utils.JumpUtils;import com.example.ruidun.ShanXunApplication.utils.UIUtils;import org.json.JSONException;import org.json.JSONObject;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import cn.jpush.android.api.JPushInterface;/** * 自定义接收器 * <p/> * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */public class MyReceiver extends BroadcastReceiver { private static final String TAG = "JPush"; private static String type = null; private static String pushId = null; private Intent myIntent; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId); //send the Registration Id to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");// int msg_id = bundle.getInt(JPushInterface.EXTRA_MSG_ID); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); String content = bundle.getString(JPushInterface.EXTRA_MESSAGE); String extra = bundle.getString(JPushInterface.EXTRA_EXTRA); //**************解析推送过来的json数据并存放到集合中 begin****************** try { JSONObject json = new JSONObject(extra); type = json.getString("type"); pushId = json.getString("pushId"); } catch (JSONException e) { e.printStackTrace(); return; } Log.d(TAG, "[MyReceiver] 接收到推送下来的通知" + "type====" + type + "pushId" + pushId); Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); Intent intentReceived = new Intent(); intentReceived.setAction(GlobalTools.HAVE_NEWS); UIUtils.getContext().sendOrderedBroadcast(intentReceived, null); ConstantTools.putAcaCheConfig(UIUtils.getContext(), GlobalTools.HAVE_NEWS, true); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用户点击打开了通知"); if (type.equals(GlobalTools.JPushType_1)) { //打开自定义的Activity myIntent = new Intent(context, HomeDetailActivity.class); myIntent.putExtra("dsId", pushId); myIntent.putExtra("type", GlobalTools.releaseType.need);// intent.putExtras(bundle);// myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_2)) {// JumpUtils.typeToActivity(getActivity(), NewsDetailActivity.class, "mtId", item.getMtId());// Intent intent2 = new Intent();// intent //打开自定义的Activity myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "2"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_3)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "3"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_4)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "3"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_5)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "1"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); } else if (type.equals(GlobalTools.JPushType_6)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "1"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_7)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "1"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_8)) { myIntent = new Intent(context, NewsDetailActivity.class); myIntent.putExtra("mtId", "1"); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_9)) { myIntent = new Intent(context, HomeDetailActivity.class); myIntent.putExtra("dsId", pushId); myIntent.putExtra("type", GlobalTools.releaseType.service);// intent.putExtras(bundle);// myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; } else if (type.equals(GlobalTools.JPushType_10)) { myIntent = new Intent(context, WebActivity.class); myIntent.putExtra("WebUri", pushId); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); return; }// for (int i = 0; i < map.size(); i++) {// if (map.get("key").equals(GlobalTools.JPushType_1)){// //打开自定义的Activity// Intent myIntent = new Intent(context, SetsActivity.class);// intent.putExtras(bundle);// //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);// context.startActivity(myIntent);// }// } } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用户收到到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); Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Log.d(TAG, "[MyReceiver] 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 if (key.equals(JPushInterface.EXTRA_EXTRA)) { if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) { Log.i(TAG, "This message has no Extra data"); continue; } try { JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); Iterator<String> it = json.keys(); while (it.hasNext()) { String myKey = it.next().toString(); sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]"); } } catch (JSONException e) { Log.e(TAG, "Get message extra JSON error!"); } } else { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString(); } //send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) {// if (MainActivity.isForeground) {// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);// if (!ExampleUtil.isEmpty(extras)) {// try {// JSONObject extraJson = new JSONObject(extras);// if (extraJson.length() > 0) {// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);// }// } catch (JSONException e) {//// }//// }// context.sendBroadcast(msgIntent);// } } /** * 判断应用是否运行在前台 */ public static boolean isRunningForeground(Context context) { String packageName = getPackageName(context); String topActivityClassName = getTopActivityName(context); System.out.println("packageName=" + packageName + ",topActivityClassName=" + topActivityClassName); if (packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName)) { System.out.println("---> isRunningForeGround"); return true; } else { System.out.println("---> isRunningBackGround"); return false; } } public static String getTopActivityName(Context context) { String topActivityClassName = null; ActivityManager activityManager = (ActivityManager) (context .getSystemService(Context.ACTIVITY_SERVICE)); List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager .getRunningTasks(1); if (runningTaskInfos != null) { ComponentName f = runningTaskInfos.get(0).topActivity; topActivityClassName = f.getClassName(); } return topActivityClassName; } public static String getPackageName(Context context) { String packageName = context.getPackageName(); return packageName; }}
这样基本就可以了,如果还有不懂的地方,可以看看官方的文档什么的,官方链接:https://docs.jiguang.cn/jpush/client/Android/android_guide/
如果觉得有用记得给妹子点个赞哦,么么哒
- 如何快速的实现极光推送
- iOS-极光消息推送的快速实现
- 极光推送功能的实现
- 极光推送的简单实现
- APP推送的实现-----极光推送
- APP推送的实现-----极光推送
- 快速集成极光推送
- 快速搞定极光推送
- 极光推送快速集成
- 极光推送的简单实现方法
- JPush极光推送的android实现
- 如何接入极光推送
- 极光推送服务端实现
- 极光推送,如何接收推送下来的信息。
- 极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式
- 极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式
- 一篇可以快速了解 极光推送 服务器api 的文章
- 极光推送的使用方法
- 简单搜索专题
- 长文 | 一文读懂什么是机器学习
- bool 变量的四则运算
- Mybatis-Generator自动生成dao,javaBean,映射
- Java
- 如何快速的实现极光推送
- JS == 与 === 区别
- vnc连linux系统后使远程桌面最大化
- java序列化之Serializable接口研究
- IntelliJ IDEA使用技巧——插件的安装及使用方法
- 解决 sql update 1292
- IntentService 从源码分析
- SpringMVC文件下载
- 基于Cloudera Manager5配置HIVE压缩