Android(安卓)极光推送快速接入
16lz
2021-01-24
一、关于极光推送
官网:极光推送
JPush 是一个大规模 App 推送平台,开发者集成 SDK 后,可以通过调用 API 推送消息。同时,JPush 提供可视化的 web 端控制台发送通知,统计分析推送效果。 JPush 全面支持 Android, iOS, Winphone 三大手机平台,同时支持多种消息形式:通知,自定义消息,富媒体和本地通知。
二、创建应用
注册账号,在 控制台 按要求和步骤创建应用
三、jcenter 自动集成
使用 jcenter 自动集成,不需要在项目中添加 jar 和 so,jcenter 会自动完成依赖;
在 AndroidManifest.xml 中不需要添加任何 JPush SDK 相关的配置,jcenter 会自动导入。
1、确认工程根目录 build.gradle 中配置了 jcenter 支持。一般新建的project 默认支持。
buildscript { repositories { jcenter() } ......}allprojets { repositories { jcenter() }}
2、在项目\app\build.gradle文件添加如下依赖
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 { ...... implementation 'cn.jiguang.sdk:jpush:3.1.1' // 此处以JPush 3.1.1 版本为例。 implementation 'cn.jiguang.sdk:jcore:1.1.9' // 此处以JCore 1.1.9 版本为例 ......}
3、创建自定义广播接收类
public class MyJPushReceiver extends BroadcastReceiver { private static final String TAG = "MyJPushReceiver"; public static String regId; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "[MyJPushReceiver] 广播被注册了"); try { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); Log.d(TAG, "[MyJPushReceiver] 接收Registration Id : " + regId); //send the Registration Id to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyJPushReceiver] 接收到推送下来的自定义消息(内容为): " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // 自定义消息不是通知,默认不会被SDK展示到通知栏上,极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。 // 自定义消息主要用于应用的内部业务逻辑和特殊展示需求 // 收到消息 显示通知 processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyJPushReceiver] 接收到推送下来的通知"); String extra_json = bundle.getString(JPushInterface.EXTRA_EXTRA); if (!TextUtils.isEmpty(extra_json)) Log.d(TAG, "[MyJPushReceiver] 接收到推送下来的通知附加字段" + extra_json); // 可以利用附加字段来区别Notication,指定不同的动作,extra_json是个json字符串 // 通知(Notification),指在手机的通知栏(状态栏)上会显示的一条通知信息 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Log.d(TAG, "[MyJPushReceiver] 用户点击打开了通知"); // 在这里根据 JPushInterface.EXTRA_EXTRA(附加字段) 的内容处理代码, // 比如打开新的Activity, 打开一个网页等.. //打开自定义的Activity Intent i = new Intent(context, MainActivity.class); 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); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Log.d(TAG, "[MyJPushReceiver] 用户收到到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, "[MyJPushReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Log.d(TAG, "[MyJPushReceiver] Unhandled intent - " + intent.getAction()); } } catch (Exception e) { e.printStackTrace(); } } /** * 处理自定义通告 * @param context * @param bundle */ private void processCustomMessage(Context context, Bundle bundle) { String channelID = "1"; String channelName = "channel_name"; // 跳转的Activity Intent intent = new Intent(context, MainActivity.class); intent.putExtras(bundle); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // 获得系统推送的自定义消息 String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); //适配安卓8.0的消息渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); } NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channelID); notification.setAutoCancel(true)//点击后消失 .setContentText(message) .setContentTitle("我是Title") .setSmallIcon(R.mipmap.ic_launcher) //设置通知栏消息标题的头像 .setDefaults(NotificationCompat.DEFAULT_ALL)//设置通知铃声,震动,闪光灯 .setContentIntent(pendingIntent); notificationManager.notify((int)(System.currentTimeMillis()/1000), notification.build()); }}
4、在AndroidManifest.xml中配置该极光推送的广播
5、添加必要的权限
6、初始化极光推送Sdk
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); }}
四、消息推送
在控制台设置好推送内容,点击发送预览->确定即可推送
推送完,手机即可收到:
极光推送更多应用见官网及文档:http://docs.jiguang.cn/jpush/client/Android/android_sdk/
更多相关文章
- android Toast显示消息的几种方法
- Android(安卓)Notification(Android(安卓)8.0)
- Android(安卓)IntentService源码分析
- Android线程学习 一
- Android仿QQ滑动弹出菜单标记已读、未读消息
- Broadcast Receiver
- 基于xmpp openfire smack开发之Android客户端开发[3]
- Android消息处理探秘
- Android中Handler小例子