zoukankan      html  css  js  c++  java
  • Android集成极光推送

    要说学习极光推送,个人感觉官方文档就非常好啦,但是没法,人太懒啦,为了下次能够快速的将极光推送集成到项目中,故结合之前开发的项目和官方文档记录下简单的Android集成极光推送,在这之前,先上一张简单的思维导图吧,最近一直在研究思维导图,感觉帮助还挺大的。

    先从官方文档中摘取出一些熟悉的名词,推送目标主要分为四种:

    广播推送:向所有的注册用户发送一条广播消息

    标签推送:根据属性对用户设置标签分组,向群组用户发送

    别名推送:客户端绑定用户别名,向具体的单个用户推送

    用户群体:根据JPush提供的多条件组合进行用户群组划分,实时筛选实时推送

    推送的消息形式也分为四种:

    通知:指在手机的通知栏(状态栏)上会显示的一条通知信息。通知主要用于提示用户的目的,应用于新闻内容、促销活动、产品信息、版本更新提醒、订单状态提醒等多种场景

    自定义形式:自定义消息不是通知,所以不会被SDK展示到通知栏上。其内容完全由开发者自己定义。自定义消息主要用于应用的内部业务逻辑。一条自定义消息推送过来,有可能没有任何界面显示。

    富媒体:JPush支持开发者发送图文并茂的通知,从而更好的传达信息,带来更丰富的用户互动。JPush提供了5种模板,开发者可以通过填充模板的内容,发送landing page、弹窗、信息流形式的富媒体通知。开发者还可以直接通过URL发送预先编辑好的页面。

    本地通知本地通知API不依赖于网络,无网条件下依旧可以触发;本地通知的定时时间是自发送时算起的,不受中间关机等操作的影响。本地通知与网络推送的通知是相互独立的,不受保留最近通知条数上限的限制。本地通知适用于在特定时间发出的通知,如一些Todo和闹钟类的应用,在每周、每月固定时间提醒用户回到应用查看任务

    集成极光推送主要有两种方式,一种是jcenter 自动集成,另外一种是手动集成,这里主要介绍下jcenter 自动集成,手动集成的可参考官方文档

     1:我们先在module的gradle中添加依赖

    1  compile 'cn.jiguang.sdk:jpush:3.0.3'  // 此处以JPush 3.0.3 版本为例。
    2  compile 'cn.jiguang.sdk:jcore:1.1.1'  // 此处以JCore 1.1.1 版本为例。

    2:确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。(新建project默认配置就支持)

     1 buildscript {
     2     repositories {
     3         jcenter()
     4     }
     5     ......
     6 }
     7 
     8 allprojets {
     9     repositories {
    10         jcenter()
    11     }
    12 }

    3:在module的gradle中的defaultconfig添加AndroidManifest的替换变量

     1    ndk {
     2             //选择要添加的对应cpu类型的.so库。
     3             abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
     4             // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
     5         }
     6 
     7         manifestPlaceholders = [
     8                 JPUSH_PKGNAME: applicationId,
     9                 JPUSH_APPKEY : "4f44ddfa7aa983a7df72b5b3", //JPush上注册的包名对应的appkey.
    10                 JPUSH_CHANNEL: "developer-default", //用户渠道统计的渠道名称
    11         ]

    4:在AndroidManifest中添加JPusService并配置android:process参数将PushService放在其他进程中:

     1  <!-- Required SDK 核心功能-->
     2         <!-- 可配置android:process参数将PushService放在其他进程中 -->
     3         <service
     4             android:name="cn.jpush.android.service.PushService"
     5             android:process=":pushcore"
     6             android:exported="false">
     7             <intent-filter>
     8                 <action android:name="cn.jpush.android.intent.REGISTER" />
     9                 <action android:name="cn.jpush.android.intent.REPORT" />
    10                 <action android:name="cn.jpush.android.intent.PushService" />
    11                 <action android:name="cn.jpush.android.intent.PUSH_TIME" />
    12             </intent-filter>
    13         </service>

    至此,集成极光推送完毕,那我们怎么使用呢?其实你可以下一个官方的Demo JPushExample研究研究

    1:首先我们要在Application中设置JPush调试模式和初始化SDK

     1 /**
     2  * Created by kebinran on 2017/7/20.
     3  */
     4 
     5 public class APP extends Application {
     6     @Override
     7     public void onCreate() {
     8         super.onCreate();
     9         //设置调试模式
    10         JPushInterface.setDebugMode(true);
    11         //init 初始化SDK
    12         JPushInterface.init(this);
    13     }
    14 }

    2:自定义的广播接收器并编写逻辑代码,你可以更快捷的从JPushExample中复制响应的代码在上面修改即可

      1 package com.example.kebinran.jpushdemo;
      2 
      3 import android.content.BroadcastReceiver;
      4 import android.content.Context;
      5 import android.content.Intent;
      6 import android.os.Bundle;
      7 import android.support.v4.content.LocalBroadcastManager;
      8 import android.text.TextUtils;
      9 
     10 import org.json.JSONException;
     11 import org.json.JSONObject;
     12 
     13 import java.util.Iterator;
     14 
     15 import cn.jpush.android.api.JPushInterface;
     16 
     17 /**
     18  * 自定义接收器
     19  * 
     20  * 如果不定义这个 Receiver,则:
     21  * 1) 默认用户会打开主界面
     22  * 2) 接收不到自定义消息
     23  */
     24 public class MyReceiver extends BroadcastReceiver {
     25     private static final String TAG = "JIGUANG-Example";
     26 
     27     @Override
     28     public void onReceive(Context context, Intent intent) {
     29         try {
     30             Bundle bundle = intent.getExtras();
     31             Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
     32 
     33             if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
     34                 String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
     35                 Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
     36                 //send the Registration Id to your server...
     37 
     38             } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
     39                 Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
     40                 processCustomMessage(context, bundle);
     41 
     42             } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
     43                 Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");
     44                 int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
     45                 Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
     46 
     47             } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
     48                 Logger.d(TAG, "[MyReceiver] 用户点击打开了通知");
     49 
     50                 //打开自定义的Activity
     51                 Intent i = new Intent(context, MainActivity.class);
     52                 i.putExtras(bundle);
     53                 //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     54                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
     55                 context.startActivity(i);
     56 
     57             } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
     58                 Logger.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
     59                 //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
     60 
     61             } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
     62                 boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
     63                 Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
     64             } else {
     65                 Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
     66             }
     67         } catch (Exception e){
     68 
     69         }
     70 
     71     }
     72 
     73     // 打印所有的 intent extra 数据
     74     private static String printBundle(Bundle bundle) {
     75         StringBuilder sb = new StringBuilder();
     76         for (String key : bundle.keySet()) {
     77             if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
     78                 sb.append("
    key:" + key + ", value:" + bundle.getInt(key));
     79             }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
     80                 sb.append("
    key:" + key + ", value:" + bundle.getBoolean(key));
     81             } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
     82                 if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
     83                     Logger.i(TAG, "This message has no Extra data");
     84                     continue;
     85                 }
     86 
     87                 try {
     88                     JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
     89                     Iterator<String> it =  json.keys();
     90 
     91                     while (it.hasNext()) {
     92                         String myKey = it.next().toString();
     93                         sb.append("
    key:" + key + ", value: [" +
     94                                 myKey + " - " +json.optString(myKey) + "]");
     95                     }
     96                 } catch (JSONException e) {
     97                     Logger.e(TAG, "Get message extra JSON error!");
     98                 }
     99 
    100             } else {
    101                 sb.append("
    key:" + key + ", value:" + bundle.getString(key));
    102             }
    103         }
    104         return sb.toString();
    105     }
    106     
    107     //send msg to MainActivity
    108     private void processCustomMessage(Context context, Bundle bundle) {
    109         if (MainActivity.isForeground) {
    110             String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
    111             String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
    112             Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
    113             msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
    114             if (!ExampleUtil.isEmpty(extras)) {
    115                 try {
    116                     JSONObject extraJson = new JSONObject(extras);
    117                     if (extraJson.length() > 0) {
    118                         msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
    119                     }
    120                 } catch (JSONException e) {
    121 
    122                 }
    123 
    124             }
    125             LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
    126         }
    127 
    128     }
    129 }

    别忘了在AndroidManifest中声明

     1   <!-- User defined.  For test only  用户自定义的广播接收器-->
     2         <receiver
     3             android:name=".MyReceiver"
     4             android:enabled="true"
     5             android:exported="false">
     6             <intent-filter>
     7                 <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用户注册SDK的intent-->
     8                 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required  用户接收SDK消息的intent-->
     9                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required  用户接收SDK通知栏信息的intent-->
    10                 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required  用户打开自定义通知栏的intent-->
    11                 <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
    12                 <category android:name="com.example.kebinran.jpushdemo" />
    13             </intent-filter>
    14         </receiver>
  • 相关阅读:
    【原】 POJ 1012 Joseph 约瑟夫 解题报告
    【原】 POJ 1001 Exponentiation 大整数乘法 解题报告
    POJ——1517
    毕业了,校园里走走看看——华中科技大学
    毕业答辩后
    CV编程常用的获取鼠标圈定区域的方法
    送走了GB
    POJ——2546
    奥巴马在YY21#524
    POJ——3517
  • 原文地址:https://www.cnblogs.com/bdsdkrb/p/7211674.html
Copyright © 2011-2022 走看看