注册好账号之后,看开发文档步骤创建应用。创建好的应用列表如下
点击应用配置,会看到后续代码会用到的内容,注意[应用标识],它是填项目的包名。
在主项目build.gradle中添加url
//个推
maven { url 'http://mvn.gt.igexin.com/nexus/content/repositories/releases/' }
app目录下的build.gradle中添加相关依赖-以及-配置
android{
//这里就用到图二的内容了
manifestPlaceholders = [
GETUI_APP_ID : "APPID",
GETUI_APP_KEY : "APPKEY",
GETUI_APP_SECRET : "APPSECRET"
]
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86_64","x86"
}
}
dependencies {
implementation 'com.getui:sdk:2.12.5.0'
}
主目录下的settings.gradle中添加配置
android.useDeprecatedNdk=true
(1)PushService.class
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.igexin.sdk.GTServiceManager;
public class PushService extends Service {
@Override
public void onCreate() {
super.onCreate();
GTServiceManager.getInstance().onCreate(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return GTServiceManager.getInstance().onStartCommand(this, intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return GTServiceManager.getInstance().onBind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
GTServiceManager.getInstance().onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
GTServiceManager.getInstance().onLowMemory();
}
}
(2)DemoIntentService,调用这个方法onReceiveClientId()得到cid,测试
import android.content.Context;
import android.util.Log;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
/**
* 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息
* onReceiveMessageData 处理透传消息
* onReceiveClientId 接收 cid
* onReceiveOnlineState cid 离线上线通知
* onReceiveCommandResult 各种事件处理回执
*/
public class DemoIntentService extends GTIntentService {
@Override
public void onReceiveServicePid(Context context, int i) {
}
@Override
public void onReceiveClientId(Context context, String clientid) {
Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
}
@Override
public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {
Log.e(TAG, "onReceiveMessageData -> " + "message = " + new String(gtTransmitMessage.getPayload()));
}
@Override
public void onReceiveOnlineState(Context context, boolean b) {
}
@Override
public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {
}
@Override
public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {
}
@Override
public void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage) {
}
}
以上俩个类写好,绑定这个服务【用全局Application绑定】如果不知道,看一下这里
(3)现在写绑定推送服务的类
import com.igexin.sdk.PushManager;
import org.litepal.LitePalApplication;
public class App extends LitePalApplication{
@Override
public void onCreate() {
super.onCreate();
// com.getui.demo.DemoPushService 为第三方自定义推送服务
PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);
// com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
}
}
GOOD LUCK!!!