Android个推实现(简单)

首先导入依赖

总的

maven {
            url "http://mvn.gt.igexin.com/nexus/content/repositories/releases/"
        }

在Gradleli

manifestPlaceholders = [
                GETUI_APP_ID : "p5psDP8IgVAaTCxFSoWNfA",
                GETUI_APP_KEY : "7XP9BYDNqK8nrvyQ6i1iJ2",
                GETUI_APP_SECRET : "XoPoJeILGD7TuAB3LzzcU1"
        ]
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86_64","x86"
        }
packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

//下面
implementation 'com.getui:sdk:2.13.0.0'

权限


    
    
    
    
    


在application里面

        

        
        
        
        

MainActivity里的代码

private static final int REQUEST_PERMISSION = 0;
    private Class userPushService = DemoPushService.class;


//oncreat里面
PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);

//俩个sevice

package com.example.getuidisanfang;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.FeedbackCmdMessage;
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 { private static final String TAG = "GetuiSdkDemo"; /** * 为了观察透传数据变化. */ private static int cnt; public DemoIntentService() { } @Override public void onReceiveServicePid(Context context, int pid) { Log.d(TAG, "onReceiveServicePid -> " + pid); } @Override public void onReceiveMessageData(Context context, GTTransmitMessage msg) { String appid = msg.getAppid(); String taskid = msg.getTaskId(); String messageid = msg.getMessageId(); byte[] payload = msg.getPayload(); String pkg = msg.getPkgName(); String cid = msg.getClientId(); // 第三方回执调用接口,actionid范围为90000-90999,可根据业务场景执行 boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001); Log.d(TAG, "call sendFeedbackMessage = " + (result ? "success" : "failed")); Log.d(TAG, "onReceiveMessageData -> " + "appid = " + appid + "\ntaskid = " + taskid + "\nmessageid = " + messageid + "\npkg = " + pkg + "\ncid = " + cid); if (payload == null) { Log.e(TAG, "receiver payload = null"); } else { String data = new String(payload); Log.d(TAG, "receiver payload = " + data); // 测试消息为了观察数据变化 sendMessage(data, 0); } Log.d(TAG, "----------------------------------------------------------------------------------------------"); } @Override public void onReceiveClientId(Context context, String clientid) { Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid); sendMessage(clientid, 1); } @Override public void onReceiveOnlineState(Context context, boolean online) { Log.d(TAG, "onReceiveOnlineState -> " + (online ? "online" : "offline")); } @Override public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) { } @Override public void onNotificationMessageArrived(Context context, GTNotificationMessage message) { Log.d(TAG, "onNotificationMessageArrived -> " + "appid = " + message.getAppid() + "\ntaskid = " + message.getTaskId() + "\nmessageid = " + message.getMessageId() + "\npkg = " + message.getPkgName() + "\ncid = " + message.getClientId() + "\ntitle = " + message.getTitle() + "\ncontent = " + message.getContent()); } @Override public void onNotificationMessageClicked(Context context, GTNotificationMessage message) { Log.d(TAG, "onNotificationMessageClicked -> " + "appid = " + message.getAppid() + "\ntaskid = " + message.getTaskId() + "\nmessageid = " + message.getMessageId() + "\npkg = " + message.getPkgName() + "\ncid = " + message.getClientId() + "\ntitle = " + message.getTitle() + "\ncontent = " + message.getContent()); } private void feedbackResult(FeedbackCmdMessage feedbackCmdMsg) { String appid = feedbackCmdMsg.getAppid(); String taskid = feedbackCmdMsg.getTaskId(); String actionid = feedbackCmdMsg.getActionId(); String result = feedbackCmdMsg.getResult(); long timestamp = feedbackCmdMsg.getTimeStamp(); String cid = feedbackCmdMsg.getClientId(); Log.d(TAG, "onReceiveCommandResult -> " + "appid = " + appid + "\ntaskid = " + taskid + "\nactionid = " + actionid + "\nresult = " + result + "\ncid = " + cid + "\ntimestamp = " + timestamp); } private void sendMessage(String data, int what) { Message msg = Message.obtain(); msg.what = what; msg.obj = data; //DemoApplication.sendMessage(msg); } }
package com.example.getuidisanfang;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.igexin.sdk.GTServiceManager;

/**
 * 核心服务, 继承 android.app.Service, 必须实现以下几个接口, 并在 AndroidManifest 声明该服务并配置成
 * android:process=":pushservice", 具体参考 {@link com.getui.demo.GetuiSdkDemoActivity}
 * PushManager.getInstance().initialize(this.getApplicationContext(), userPushService), 其中
 * userPushService 为 用户自定义服务 即 DemoPushService.
 */
public class DemoPushService extends Service {

    public static final String TAG = DemoPushService.class.getName();

    @Override
    public void onCreate() {
        // 该行日志在 release 版本去掉
        Log.d(TAG, TAG + " call -> onCreate -------");

        super.onCreate();
        GTServiceManager.getInstance().onCreate(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 该行日志在 release 版本去掉
        Log.d(TAG, TAG + " call -> onStartCommand -------");

        super.onStartCommand(intent, flags, startId);
        return GTServiceManager.getInstance().onStartCommand(this, intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // 该行日志在 release 版本去掉
        Log.d(TAG, "onBind -------");
        return GTServiceManager.getInstance().onBind(intent);
    }

    @Override
    public void onDestroy() {
        // 该行日志在 release 版本去掉
        Log.d(TAG, "onDestroy -------");

        super.onDestroy();
        GTServiceManager.getInstance().onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        GTServiceManager.getInstance().onLowMemory();
    }
}

在个推网页里把该改的类似于appkey这种的改改即可

你可能感兴趣的:(知识点)