极光推送

来源:互联网 发布:知乎怎样查航班动态 编辑:程序博客网 时间:2024/05/21 09:46

上午写了百度地图定位的,顺便把极光也写上去

第一步大家都知道

官方资源

官方文档: 
http://docs.jiguang.cn/client/android_sdk/ 
3 分钟快速集成 JPush Android SDK: 
http://docs.jiguang.cn/guideline/android_3m/ 
Android SDK集成: 
http://docs.jiguang.cn/guideline/android_guide/

我写得相对于简单

先到官网去申请appkey,得到KEY后

SDK配置

Gradle配置(Module的build.gradle)
首先,确保你的minSdkVersion在15及以上版本,然后defaultConfig中加入

ndk {    //选择要添加的对应cpu类型的.so库。    abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'}manifestPlaceholders = [        JPUSH_PKGNAME: applicationId,        JPUSH_APPKEY : "***************",//你在极光推送的KEY        JPUSH_CHANNEL: "developer-default",]


 

是不是觉得很简单,没有必要下载包

添加依赖
还是Module的build.gradle文件中哦
 compile 'cn.jiguang.sdk:jpush:3.0.1'
 compile 'cn.jiguang.sdk:jcore:1.1.0'
至此,只需同步一下即可自动配置sdk


报错的话就这样子弄

在Project的gradle.properties文件中添加下面的代码:<喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> android.useDeprecatedNdk=true

大家都懂的


混淆
在ProGuard文件(即proguard-rules.pro)中加入混淆代码:


-dontoptimize
-dontpreverify
 
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }


初始化
新建一个MyApplication类(继承至Application),并在清单文件(AndroidManifest.xml)的application节点添加name属性关联MyApplication。在MyApplication的onCreate方法中加入初始化的代码:




public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化sdk
        JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
        JPushInterface.init(this);
        //建议添加tag标签,发送消息的之后就可以指定tag标签来发送了
        Set<string> set = new HashSet<>();
        set.add("andfixdemo");//名字任意,可多添加几个
        JPushInterface.setTags(this, set, null);//设置标签
    }
}</string>
创建自定义广播接收器
极光推送是需要自定义一个广播器来接收推送消息的。




/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyPushReceiver extends BroadcastReceiver {
    private static final String TAG = MyPushReceiver.class.getSimpleName();
 
    @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_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
 
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            processCustomMessage(context, bundle);
 
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
 
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
//
//          //打开自定义的Activity
//          Intent i = new Intent(context, TestActivity.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, "[MyReceiver] 用户收到到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, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }
 
    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
      
    }
 
    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
      
    }
}


清单文件大家懂的


<receiver    android:name=".push.MyPushReceiver"    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.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->        <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->        <category android:name="com.shengzhe.disan.xuetangparent" />    </intent-filter>

还有就是权限了

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="${applicationId}.permission.JPUSH_MESSAGE" /><uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.WRITE_SETTINGS" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><permission    android:name="${applicationId}.permission.JPUSH_MESSAGE"    android:protectionLevel="signature" />



是不是感觉超简单,