Android使用XPush配置极光推送
Android使用XPush配置极光推送
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
导入依赖
这里不做赘述https://gitee.com/xuexiangjys/XPush 仔细阅读xpush作者说明文档
AndroidManifest.xml
<!-- 自定义消息推送接收器 -->
<receiver android:name=".push.CustomPushReceiver">
<intent-filter>
<action android:name="com.xuexiang.xpush.core.action.RECEIVE_CONNECT_STATUS_CHANGED" />
<action android:name="com.xuexiang.xpush.core.action.RECEIVE_NOTIFICATION" />
<action android:name="com.xuexiang.xpush.core.action.RECEIVE_NOTIFICATION_CLICK" />
<action android:name="com.xuexiang.xpush.core.action.RECEIVE_MESSAGE" />
<action android:name="com.xuexiang.xpush.core.action.RECEIVE_COMMAND_RESULT" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<!-- 极光推送静态注册 -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="你自己的Secret" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="你自己的APPKE" />
创建自定义消息接收器CustomPushReceiver–对应AndroidManifest.xml的自定义消息接收器下面展示一些 内联代码片
。
package com.example.zhaopz.push;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import com.example.zhaopz.BuildConfig;
import com.example.zhaopz.activity.MainActivity;
import com.example.zhaopz.activity.TestMsgActivity;
import com.xuexiang.xpush.XPush;
import com.xuexiang.xpush.core.IPushInitCallback;
import com.xuexiang.xpush.core.receiver.impl.XPushReceiver;
import com.xuexiang.xpush.entity.XPushCommand;
import com.xuexiang.xpush.entity.XPushMsg;
import com.xuexiang.xpush.jpush.JPushClient;
public class CustomPushReceiver extends XPushReceiver {
@Override
public void onNotificationClick(Context context, XPushMsg msg) {
super.onNotificationClick(context, msg);
/*点击通知栏进入处理推送消息的页面*/
//Intent intent = new Intent(context, TestMsgActivity.class);
//context.startActivity(intent);
}
@Override
public void onCommandResult(Context context, XPushCommand command) {
super.onCommandResult(context, command);
//ToastUtils.toast(command.getDescription());
}
}
在Application初始化XPush
下面展示一些 内联代码片
。
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//我的项目使用了XUI这里是对XUI进行的初始化大家随意
XUI.init(this); //初始化UI框架
XUI.debug(true); //开启UI框架调试日志
//xpush初始化
initPush();
}
/**
* 静态注册初始化推送
*/
private void initPush() {
XPush.debug(BuildConfig.DEBUG);
//静态注册,指定使用友盟推送客户端
XPush.init(this, new JPushClient());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Android8.0静态广播注册失败解决方案一:动态注册
//XPush.registerPushReceiver(new CustomPushReceiver());
//Android8.0静态广播注册失败解决方案二:修改发射器
XPush.setIPushDispatcher(new Android26PushDispatcherImpl(CustomPushReceiver.class));
}
XPush.register();
}
}
接收消息页面-写在接收消息的页面
下面展示一些 内联代码片
。
//注册推送---在 onCreate或者onStart
XPushManager.get().register(mMessageSubscriber);
仔细看mMessageSubscriber 它是一个内部类!!!
//绑定别名精准推送--在在 onCreate或者onStart
通过调用XPush.bindAlias('自己创建的应用别名'),即可绑定别名。
//按照别名进行精准推送--这里通过调用XPush.unBindAlias(),即可解绑别名----调用方法解除绑定即可
//XPush.unBindAlias("887744556211");
//开启推送相关
private MessageSubscriber mMessageSubscriber = new MessageSubscriber() {
@Override
public void onMessageReceived(CustomMessage message) {
showMessage(String.format("收到自定义消息:%s", message));
}
@Override
public void onNotification(Notification notification) {
showMessage(String.format("收到通知:%s", notification));
}
};
//接收到推送过来的消息
private void showMessage(String msg) {
System.out.println(msg+"----这是信息信息**********************");
}
//跟随页面销毁XPush
@Override
public void onDestroy() {
XPushManager.get().unregister(mMessageSubscriber);
super.onDestroy();
}
到这里你的app就可以接收到来自jpush的推送消息了!!