推送-极光推送

292 阅读1分钟

推送初识

根据郭霖大神的视频学习

推送: 服务器定向将消息实时发送给客户端。
这里写图片描述

长连接:服务器与客户端保持连接
短连接:服务器与客户端在发送数据时才连接

开始第一个推送: 极光推送

通过极光官网的一个三分钟的demo了解极光推送的流程

推送通知: 通知会在通知栏显示

推送消息:只能在logcat查询

  (1)极光推送的步骤, 首先将sdk中的jar和.so文件拷贝到本项目中的libs中
以下代码定制一个本应用程序 Application 类。
需要在 AndoridManifest.xml 里配置。
sdk的初始化在Application中 .


import cn.jpush.android.api.JPushInterface;
import android.app.Application;

public class ExampleApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        //设置调试模式
        JPushInterface.setDebugMode(true);
        //初始化
        JPushInterface.init(this);
    }
}

android:name=”com.chb.jpushdemo.ExampleApplication”>

    <application
        android:icon="@drawable/ic_launcher"
        android:allowBackup="true"
        android:label="@string/app_name"
      android:name="com.chb.jpushdemo.ExampleApplication">    

定义一个自定义广播接收者,用来接收推送的广播:处理相应的通知和消息:


        <!-- User defined.  For test only  用户自定义的广播接收器-->
        <receiver
            android:name="com.chb.jpushdemo.MyReceiver"
            android:exported="false"
            android:enabled="true">
            <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.chb.jpushdemo" />
            </intent-filter>
        </receiver>

在自定义广播中的处理通知:

package com.chb.jpushdemo;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
/**
 * 接收推送广播,
 * @author Administrator
 *
 */
public class MyReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d("tag", "onReceive - " + intent.getAction());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏,完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了通知");
            // 在这里可以做些统计,或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            System.out.println("用户点击打开了通知");
            // 在这里可以自己写代码去定义用户点击后的行为
            Intent i = new Intent(context, MainActivity.class);  //自定义打开的界面
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else {
            Log.d("tag", "Unhandled intent - " + intent.getAction());
  }
}


}