android 接极光推送

步骤 1 在app 的build.gradle 

// 极光相关配置

manifestPlaceholders = [

JPUSH_PKGNAME :applicationId,

JPUSH_APPKEY : JPUSH_APPKEY,

JPUSH_CHANNEL :"developer-default",//暂时填写默认值即可.

]

dependencies{

implementation'cn.jiguang.sdk:jpush:3.8.6' // 此处以JPush 3.8.6 版本为例。

implementation'cn.jiguang.sdk:jcore:2.6.0'  // 此处以JCore 2.6.0 版本为例。

}


2 在 androidmainfest.xml 

    android:name=".service.JPushIntentService"

    android:enabled="true"

    android:exported="false"

    android:process=":pushcore">

        <action android:name="cn.jiguang.user.service.action" />


<receiver android:name=".receiver.PushMessageReceiver">

        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />

        <category android:name="${applicationId}" />

</receiver>

新建 PushMessageReceiverextends

public class PushMessageReceiverextends JPushMessageReceiver {

private static final StringTAG ="JIGUANG";

    /**

* TODO 连接极光服务器

*/

    @Override

    public void onConnected(Context context, boolean b) {

super.onConnected(context, b);

        Log.e(TAG, "onConnected");

    }

/**

* TODO 注册极光时的回调

*/

    @Override

    public void onRegister(Context context, String s) {

super.onRegister(context, s);

        Log.e(TAG, "onRegister" + s);

    }

/**

* TODO 注册以及解除注册别名时回调

*/

    @Override

    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {

super.onAliasOperatorResult(context, jPushMessage);

        Log.e(TAG, jPushMessage.toString());

    }

/**

* TODO 接收到推送下来的通知

*      可以利用附加字段(notificationMessage.notificationExtras)来区别Notication,指定不同的动作,附加字段是个json字符串

*      通知(Notification),指在手机的通知栏(状态栏)上会显示的一条通知信息

*/

    @Override

    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {

super.onNotifyMessageArrived(context, notificationMessage);

        Log.e(TAG, notificationMessage.toString());

    }

/**

* TODO 打开了通知

*      notificationMessage.notificationExtras(附加字段)的内容处理代码

*      比如打开新的Activity, 打开一个网页等..

*/

    @Override

    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {

super.onNotifyMessageOpened(context, notificationMessage);

        Log.e(TAG, notificationMessage.notificationExtras);

    }

/**

* TODO 接收到推送下来的自定义消息

*      自定义消息不是通知,默认不会被SDK展示到通知栏上,极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。

*      自定义消息主要用于应用的内部业务逻辑和特殊展示需求

*/

    @Override

    public void onMessage(Context context, CustomMessage customMessage) {

super.onMessage(context, customMessage);

        Log.e(TAG, "onMessage");

        JSONObject obj = JSON.parseObject(customMessage.message);

        Logger.t("接收到的推送内容").json(JSON.toJSONString(obj));

}

}


新建 JPushIntentServiceextends

public class JPushIntentServiceextends JCommonService {

}

最后 确认下你的 根目录 build.gradle  是否有 jcenter 没有就加上

buildscript {

    repositories {

        jcenter()

    }

    ......

}

allprojects {

    repositories {

        jcenter()

    }

}


好了 代码就上面, 你要是拿到自定义消息   public void onMessage(Context context, CustomMessage customMessage) { }在这个里面写  

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容