Android中接入极光推送

Android中接入极光推送的步骤大体如下:

1.在主module的build.gradle中添加:

android {
    defaultConfig {
        manifestPlaceholders = [
            // app包名
            JPUSH_PKGNAME: "com.jiguang.test",
            // JPush上注册的包名对应的appkey
            JPUSH_APPKEY : "898f0d6299c1666751ca40a1",
            // 道通,暂时填写默认值即可
            JPUSH_CHANNEL: "developer-default",
        ]
    }
}
dependencies {
    ......
    implementation 'cn.jiguang.sdk:jpush:3.5.8'
    implementation 'cn.jiguang.sdk:jcore:2.3.0'
    ......
}

2.在主module的AndroidManifest.xml中添加:

<!-- 极光推送 begin -->
<!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
<!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
<service
    android:name=".service.JPushService"
    android:enabled="true"
    android:exported="false"
    android:process=":pushcore">
    <intent-filter>
        <action android:name="cn.jiguang.user.service.action" />
    </intent-filter>
</service>
<!-- Required since 3.0.7 -->
<!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 -->
<!-- 3.3.0开始所有事件将通过该类回调 -->
<!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
<!-- category中的name为app的包名 -->
<receiver
    android:name=".receiver.JPushReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
        <category android:name="com.jiguang.test" />
    </intent-filter>
</receiver>
<!-- 极光推送 end -->

3.自定义JCommonService类:

package com.jiguang.test.service;

import cn.jpush.android.service.JCommonService;

/**
 * 极光推送服务
 */
public class JPushService extends JCommonService {
    // 空实现即可
}

4.自定义JPushMessageReceiver类:

package com.jiguang.test.receiver;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * 极光推送的广播接收器
 */
public class JPushReceiver extends JPushMessageReceiver {
    private static final String TAG = JPushReceiver.class.getSimpleName();

    public JPushReceiver() {
    }

    /**
     * 自定义消息接收
     *
     * @param context       上下文
     * @param customMessage 自定义消息
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.d(TAG, "onMessage customMessage=" + customMessage);
    }

    /**
     * 通知消息接收
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageArrived notificationMessage=" + notificationMessage);
    }

    /**
     * 点击通知消息
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageOpened notificationMessage=" + notificationMessage);
    }

    /**
     * 清除通知消息
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageDismiss notificationMessage=" + notificationMessage);
    }

    /**
     * 注册成功
     *
     * @param context        上下文
     * @param registrationId 注册id
     */
    @Override
    public void onRegister(Context context, String registrationId) {
        Log.d(TAG, "onRegister registrationId=" + registrationId);
    }

    /**
     * 长连接状态变化
     *
     * @param context     上下文
     * @param isConnected 长连接状态,true表示已连接
     */
    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.d(TAG, "onConnected isConnected=" + isConnected);
    }
}

5.在主module的Application类的onCreate方法中添加:

JPushInterface.setDebugMode(true); // debug开关,传true是打开debug模式
JPushInterface.init(this);


 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值