第三方推送

1.在APP下的build中进行添加

并在build中进行依赖

//信鸽jar
implementation 'com.tencent.xinge:xinge:3.2.6-release'
//jg包
implementation'com.tencent.jg:jg:1.1'
//wup包
implementation 'com.tencent.wup:wup:1.0.0.E-release'
//mid包
implementation 'com.tencent.mid:mid:4.0.6-release'

2.在清单文件中添加

<receiver
    android:name="你自己创建的receiver"
    android:exported="true">
    <intent-filter>
        <!-- 接收消息透传 -->
        <action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" />
        <!-- 监听注册、反注册、设置/删除标签、通知被点击等处理结果 -->
        <action android:name="com.tencent.android.tpush.action.FEEDBACK" />
    </intent-filter>
</receiver>

3.创建receiver

记住继承的是XGPushBaseReceiver

public class Receiver extends XGPushBaseReceiver {

    @Override
    public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {

    }

    @Override
    public void onUnregisterResult(Context context, int i) {

    }

    @Override
    public void onSetTagResult(Context context, int i, String s) {

    }

    @Override
    public void onDeleteTagResult(Context context, int i, String s) {

    }

    @Override
    public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {

    }

    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult xgPushClickedResult) {

    }

    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {

    }
}

4.创建APP继承application

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        XGPushConfig.enableDebug(this, true);
        XGPushManager.registerPush(this, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
//token在设备卸载重装的时候有可能会变
                Log.d("TPush", "注册成功,设备token为:" + data);
            }

            @Override
            public void onFail(Object data, int errCode, String msg) {
                Log.d("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg);
            }
        });


    }
}

5.在腾讯移动推送页面中实现推送

完成推送

扫描二维码关注公众号,回复: 3685437 查看本文章

附加实现手动推送

可以用点击事件事项手动推送

private void show() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("用户更新了")//设置通知栏标题
            .setContentText("新版本来了!!!!")
            .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL, this)) //设置通知栏点击意图
            .setTicker("新版本来了!!!!") //通知首次出现在通知栏,带上升动画效果的
            .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
            .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
            .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
            .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
            .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
            .setSound(Uri.withAppendedPath(
                    MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "5"))
            .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON
    mNotificationManager.notify(3, mBuilder.build());
}

private PendingIntent getDefalutIntent(int flagAutoCancel, MainActivity mainActivity) {
    //
    return null;
}

猜你喜欢

转载自blog.csdn.net/a1078058302/article/details/83176188