初探极光推送(一)

来源:互联网 发布:windows病毒 编辑:程序博客网 时间:2024/06/05 21:11

记得以前面试的时候有个面试官说用要多熟悉一些第三方的SDK还特别说到了极光推送,当时只是知道极光推送是做推送用的,具体怎么用不知道!时隔八个月今天来看看怎么在项目中集成极光推送功能。
首先下载极光推送
解压以后文件列表如下:

这里写图片描述
把libs和res里面的文件复制到自己工程下。
AndroidManifest文件分Android Studio和Eclipse这个需要区分下,我们以Android Studio为例,不能一味的复制里面的代码这里面关于Activity的代码不用复制,还有两个地方需要我们自己改一下
一:
这里写图片描述
把${applicationId}改成自己的包名;
二:
这里写图片描述
这里改成自己在极光推送上申请的key,具体怎么申请的就不做介绍了。
接下来在新建一个MyApplication并在中初始化JPush

public class MyApplication extends Application {    private static final String TAG = "JPushDemo";    @Override    public void onCreate() {        super.onCreate();        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志        JPushInterface.init(this);            // 初始化 JPush    }}

记得在AndroidManifest里配置。
这篇文章只是实现接收推送消息并显示推送的自定义小时在界面上。
个人觉得极光推送的SDK很人性化,之所以这么说是因为刚刚开始只是测试下接受推送的消息,细节方面没有注意到比如在onPause()中没有调用 JPushInterface.onPause(),和onResume()中没有调用JPushInterface.onResume();然后直接运行了,这时候会收到一个推送消息,提示我们在onPause()和onResume()方法中没有调用上面说的这两个方法,点开通知以后:
这里写图片描述
是不是很人性化呀 。
在MainActivity中写一个BroadcastReceiver来接收自定义消息的内容

public class MainActivity extends Activity {    private TextView content;    private Receive mMessageReceiver;    public static final String MESSAGE_RECEIVED_ACTION = "com.example.lzh.jpushdemo.MESSAGE_RECEIVED_ACTION";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        content = (TextView) findViewById(R.id.content);        registerReceiver();    }    //注册BroadcastReceiver     private void registerReceiver() {        mMessageReceiver = new Receive();        IntentFilter filter = new IntentFilter();        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);         filter.addAction(MESSAGE_RECEIVED_ACTION);        registerReceiver(mMessageReceiver, filter);    }    @Override    protected void onResume() {        super.onResume();        JPushInterface.onResume(this);    }    @Override    protected void onPause() {        super.onPause();        JPushInterface.onPause(this);    }    class Receive extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            content.setText(intent.getStringExtra("content"));        }    }}

再新建一个BroadcastReceiver来接受极光推送的通知和自定义消息:

public class MyReceiver extends BroadcastReceiver {    private static final String TAG = "JPush";    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);            msgIntent.putExtra("content", message);            context.sendBroadcast(msgIntent);        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);        }    }}

在下载SDK的时候example里面有相应的代码,看下demo就差不多了,我这里基本是直接拿来用的,需要在AndroidManifest配置

        <receiver            android:name="com.example.lzh.jpushdemo.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.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="${applicationId}" />            </intent-filter>        </receiver>

这样就能在我们的app中实现了接收极光推送的通知和自定义消息了,至于怎么推送的,这个当然是在极光的控制台咯。
这里写图片描述

这里写图片描述

0 0