Android利用云推送,实现实时更新评论、动态

使用背景:

本人做了一个类似QQ空间的DEMO,可以发表动态等等,但是苦于没办法实时更新是否有人发了动态,然后要通知其他用户有了新动态!然后评论也无法做到实时提示!所以我想到了即时通讯,但是这真的大材小用了,所以想来想去还是推送比较好。
我使用的是极光推送,可以做到在一个App里面向所有的人发送广播,实现实时通知。

搭建推送(95%copy就行了)

首先,最好去极光注册一个账号,弄一个APPKey比较好,或者直接用我提供的试试也行。

  1. gradle要compile的东西:
 compile 'cn.jiguang.sdk:jpush:3.0.1' 
 compile 'cn.jiguang.sdk:jcore:1.1.0'
 compile 'com.google.code.gson:gson:2.6.2'
  1. 在gradle的defaultConfig里面加入:
defaultConfig {
            manifestPlaceholders = [
                    JPUSH_PKGNAME : applicationId,
                    JPUSH_APPKEY : "001b78b1bcce3110305799ec", //JPush上注册的包名对应的appkey.
                    JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
            ]
            ndk {
                //选择要添加的对应cpu类型的.so库。
                abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a','x86', 'x86_64', 'mips', 'mips64'
            }
        }
  1. libs文件夹要包含的文件:
    jpush-client-2.0.1.jar

  2. AndroidManifest的<application>里面(直接copy就好,但是有一个自己定义的receiver):

    <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:process=":mult"
             tools:replace="android:process">
             <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>
    
         <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
         <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
         <service
             android:name="cn.jpush.android.service.DaemonService"
             android:enabled="true"
             android:exported="true">
             <intent-filter>
                 <action android:name="cn.jpush.android.intent.DaemonService" />
    
                 <category android:name="com.ice.timecollector" />
             </intent-filter>
         </service>
    
         <!-- Required SDK核心功能 -->
         <receiver
             android:name="cn.jpush.android.service.PushReceiver"
             android:enabled="true">
             <intent-filter android:priority="1000">
                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!-- Required  显示通知栏 -->
                 <category android:name="com.ice.timecollector" />
             </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.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.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                 <category android:name="com.ice.timecollector" />
             </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="001b78b1bcce3110305799ec" /> <!-- </>值来自开发者平台取得的AppKey -->
    
  3. 新建一个广播接收器(在这里,你将接收到推送的消息,然后你就可以通过广播或者EventBus等方式,去实时提醒用户,有新动态啦等等):

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @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_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //这个是接受的自定义消息  也就是APP发出来的消息
            //通过获取bundle的各种参数来进行响应
            //最好通过EventBus来进行跨组件的消息传递!非常方便
        }
    }

    // 打印所有的 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();
    }
}
  1. 接下来是发送通知的“服务端”:
public class JPushClientExample {
    private static final String appKey ="001b78b1bcce3110305799ec"; //必填
    private static final String masterSecret = "87f911b23ce7b84109f1b6f8";//必填,每个应用都对应一个masterSecret
    private static JPushClient jpush = null;
    /**
     * 保存离线的时长。秒为单位。最多支持10天(864000秒)。
     * 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
     * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。
     */
    private static long timeToLive = 60 * 60 * 24;
    //测试发送数据
    public static void main(String[] args) {
        jpush = new JPushClient(masterSecret, appKey, timeToLive);
        Timer timer = new Timer();
        //在1秒后执行此任务,每次间隔半小时,如果传递一个Data参数,就可以在某个固定的时间执行这个任务
        timer.schedule(new MyTask(),0);
    }
    private static class MyTask extends TimerTask {
        @Override
        public void run() {
            testSend(null);
        }
    }
    private static void testSend(Map<String, String> map) {
        // 在实际业务中,建议 sendNo 是一个你自己的业务可以处理的一个自增数字。
        // 除非需要覆盖,请确保不要重复使用。详情请参考 API 文档相关说明。
        int sendNo = getRandomSendNo();
        String href = "this is href";
        String msgTitle = "this is msgTitle";
        String msgContent="this is msgContent";
        String url = "this is url";    //图片地址

        Map<String, Object> extra = new HashMap<String, Object>();
        extra.put("href", href);
        extra.put("src", url);
        //  IOSExtra iosExtra = new IOSExtra(10, "WindowsLogonSound.wav");
        //  extra.put("ios", iosExtra);

        //对所有用户发送通知, 更多方法请参考文档    message字段
        MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo, msgTitle, msgContent, "a", extra);  //发送自定义消息

        if (null != msgResult) {
            System.out.println("服务器返回数据: " + msgResult.toString());
            if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
                System.out.println(String.format("发送成功, sendNo= %s,messageId= %s",msgResult.getSendno(),msgResult.getMsg_id()));
            } else {
                System.out.println("发送失败, 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
            }
        } else {
            System.out.println("无法获取数据");
        }
    }

    public static final int MAX = Integer.MAX_VALUE;
    public static final int MIN = (int) MAX/2;

    /**
     * 保持 sendNo 的唯一性是有必要的
     * It is very important to keep sendNo unique.
     * @return sendNo
     */
    public static int getRandomSendNo() {
        return (int) (MIN + Math.random() * (MAX - MIN));
    }
}
  1. 运行APP,然后运行main方法:
    Log打印结果如下:
[MyReceiver] onReceive - cn.jpush.android.intent.MESSAGE_RECEIVED, extras: 
key:cn.jpush.android.EXTRA, value: [src - this is url]
key:cn.jpush.android.EXTRA, value: [href - this is href]
key:cn.jpush.android.TITLE, value:this is msgTitle
key:cn.jpush.android.MESSAGE, value:this is msgContent
key:cn.jpush.android.CONTENT_TYPE, value:a
key:cn.jpush.android.APPKEY, value:001b78b1bcce3110305799ec
key:cn.jpush.android.MSG_ID, value:2862869409

我们可以看出来,我们在main方法里面放置的参数都打印出来了,所以我们完全我可以放入一些项目要用的参数,然后进行实时提醒用户。
举个例子:当用户发了一条动态,我们就可以调用send函数。在receiver里面要写好处理的逻辑,当收到消息后就更新动态列表!
如果你搭建失败了,请在下面评论!

    原文作者:CSU_IceLee
    原文地址: https://www.jianshu.com/p/0cb2010f6041
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞