android极光推送

1.注册账号
2.添加应用,得到 AppKey,如29e2684959f57314baa4b931 
3.设置包名,图标等信息
4.在moudle的build.gradle文件的default中添加
 manifestPlaceholders = [
                JPUSH_PKGNAME: 'com.example.testpush',
                JPUSH_APPKEY : "29e2684959f57314baa4b931", //JPush上注册的包名对应的appkey(*换成你的*)
                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
        ]
 并添加依赖:
 compile 'cn.jiguang:jpush:2.1.8'  // 此处以SDK 2.1.8版本为例
 5.创建Application
 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);//设置标签

    }
}
6.在AndroidManifest.xml中添加
<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            tools:node="replace">
            <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="你的包名" />
            </intent-filter>
        </receiver>
 7.广播接收
 public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = MyReceiver.class.getSimpleName();
    public static String regId;

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            Log.e(TAG, "action===="+intent.getAction());
            Log.e(TAG, "data======"+printBundle(bundle));

            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                //send the Registration Id to your server...
                regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                Log.e(TAG, "Registration Id====="+regId);

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                // 对应极光后台的 - 自定义消息  默认不会出现在notification上 所以一般都选用发送通知
                Log.e(TAG, "接收到推送下来的自定义消息====="+bundle.getString(JPushInterface.EXTRA_MESSAGE));

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                int notification = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                Log.e(TAG, "接收推送下来的通知id======"+notification);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.e(TAG, "用户点击打开了通知");

                //打开自定义的Activity
                //Intent i = new Intent(context,ContentActivity.class);
                //i.putExtras(bundle);
                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //context.startActivity(i);

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                //根据JPushInterface.EXTRA_EXTRA的内容处理代码,打开新的Activity或者一个网页等
                Log.e(TAG, "用户收到到RICH PUSH CALLBACK:====== " + bundle.getString(JPushInterface.EXTRA_EXTRA));

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                Log.e(TAG, "connected state change to =======" + connected);
            } else {
                Log.e(TAG, "Unhandled intent======" + intent.getAction());
            }

        } catch (Exception e) {

        }


    }

    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 (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {

                    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();
    }
}

猜你喜欢

转载自blog.csdn.net/ronadlo7/article/details/81097312