在androidStudio上利用依赖注入集成极光推送的步骤。

来源:互联网 发布:淘宝文具店怎么推广 编辑:程序博客网 时间:2024/06/18 12:44
在androidStudio上利用依赖注入集成的极光推送的步骤
1.新建一个android工程,打开build.gradle(Module:app)
添加jpush依赖:compile 'cn.jiguang:jpush:2.1.8'
然后修改以下代码:
android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.example.administrator.android_school_system"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        ndk {
            //选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }
        manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY : "454554545464646555", //JPush上注册的包名对应的appkey.
                JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
        ]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
2.创建一个MyApplication继承自Application,把mainfast中的Application改成MyApplication。在MyApplication的oncreate方法中初始化极光推送
//设置极光推送的调试模式
    JPushInterface.setDebugMode(true);
//初始化极光推送
JPushInterface.init(this);
3.自己写个BroadCastReceiver的类,如下:
public class JpushReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    private NotificationManager nm;
    private Context _context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this._context = context;
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + bundle);
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "接受到推送下来的自定义消息"+bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //  把消息取出来,然后打印
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Log.d(TAG, "接受到推送下来的自定义消息"+message);
           
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");
            receivingNotification(context,bundle);


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


    private void receivingNotification(Context context, Bundle bundle){
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Log.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        Log.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Log.d(TAG, "extras : " + extras);
    }
}
然后启动应用就可以在极光开发者平台上发信息测试了。
0 0
原创粉丝点击