随着Android系统不断升级,应用后台存活时间一直受限制,所以通过应用进程的NotificationManager
弹出通知的方式并不可靠。
但是应用不存活,也要弹出通知的需求一直在。Android不像IOS系统,IOS支持apns推送服务,所以天然是可靠的。为了解决这个问题,各个手机厂商推出了自己的官方推送。
推送分为两种:
- 透传推送,依赖应用进程的存活
- 官方推送,由厂商推送,不依赖应用进程的存活
本文以 HUAWEI MI VIVO OPPO 四个手机厂商为例,分别贴出相应的接入须知和实现代码。
每个厂商都要由公司的运营或者专门的同学帮我们申请,当然,如果公司规模较小,那么只能由开发自己去申请。其他三家还好,OPPO的官方推送申请是有条件,其中有一条是即时通讯的APP不能接入。(大家接入时,要仔细阅读文档)
HUAWEI
按照如上文档,引入华为的sdk。
AndroidManifest
<!-- 接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(注册、Push消息、Push连接状态)广播,
此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类,
参考示例代码中的类:com.huawei.hmsagent.HuaweiPushRevicer
Access to the HMSSDK push module requires registration:
Receive push message (registration, push message, push connection state) broadcast.
This receiver class requires the developer to create and inherit the com.huawei.hms.support.api.push.PushReceiver class.
Reference to class in sample code: Com.huawei.hmsagent.HuaweiPushRevicer-->
<receiver android:name=".push.HuaWeiPushReceiver">
<intent-filter>
<!-- 必须,用于接收token | Must, for receiving token -->
<action android:name="com.huawei.android.push.intent.REGISTRATION"/>
<!-- 必须,用于接收消息 | Must, used to receive messages-->
<action android:name="com.huawei.android.push.intent.RECEIVE"/>
<!-- 可选,用于点击通知栏或通知栏上的按钮后触发onEvent回调 | Optional, click the button on the notification bar or the notification bar to trigger the onevent callback -->
<action android:name="com.huawei.android.push.intent.CLICK"/>
<!-- 可选,查看push通道是否连接,不查看则不需要 | Optional, query whether the push channel is connected or not -->
<action android:name="com.huawei.intent.action.PUSH_STATE"/>
</intent-filter>
</receiver>
HuaWeiPushReceiver
- 用来记录华为推送的token,以便于告知自己的服务
- 接受透传推送
public class HuaWeiPushReceiver extends PushReceiver {
private static final String TAG = "HuaWeiPushReceiver";
public static String getToken() {
return tokenStore.get();
}
private static final PreferenceStore.StringStore tokenStore = PreferenceStore.ofString("sun.push.huawei.token", "");
@Override
public void onToken(Context context, String token) {
super.onToken(context, token);
if (PushInit.PUSH_LOG) {
Log.i(TAG, "onToken() called with: context = [" + context + "], token = [" + token + "]");
}
Logger.i(true, "华为推送 token: %s", token);
if (!TextUtils.isEmpty(token)) {
tokenStore.set(token);
}
PushRegisterProxy.registerHuaWeiPush();
}
@Override
public boolean onPushMsg(Context context, byte[] msgBytes, Bundle extras) {
if (PushInit.PUSH_LOG) {
Log.i(TAG, "onPushMsg() called with: context = [" + context + "], msgBytes = [" + new String(msgBytes, Chars.UTF_8) + "], extras = [" + extras + "]");
}
final String msg = new String(msgBytes, Chars.UTF_8);
PassThroughPushCenter.onReceivePush(msg, PassThroughPushCenter.TYPE_HUAWEI);
return true;
}
@Override
public void onEvent(Context context, Event event, Bundle extras) {
super.onEvent(context, event, extras);
if (PushInit.PUSH_LOG) {
Log.i(TAG, "onEvent() called with: context = [" + context + "], event = [" + event + "], extras = [" + extras + "]");
}
}
@Override
public void onPushState(Context context, boolean pushState) {
super.onPushState(context, pushState);
if (PushInit.PUSH_LOG) {
Log.i(TAG, "onPushState() called with: context = [" + context + "], pushState = [" + pushState + "]");
}
}
}
HuaWeiPushDispatcher
当点击华为的官方推送,会显示此Activity,点击后的操作,都由HuaWeiPushDispatcher
触发。
public class HuaWeiPushDispatcher extends Activity {
private static final String TAG = "HuaWeiPushDispatcher";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
final Intent intent = getIntent();
final String data = intent.getStringExtra("data");
final BasePushMsgEntity model = Jsons.parseJson(data, BasePushMsgEntity.class);
Log.i(TAG, "onCreate: " + data);
if (model == null || !model.isValid()) {
finish();
return;
}
NotifyPushCenter.onNotifyPush(this, data, model, NotifyPushCenter.NotifyPushChannel.HwPush);
new Handler().post(new Runnable() {
@Override
public void run() {
finish();
}
});
}
}
很简单,我们通过intent获取到推送过来的数据,然后处理。最后将此Activity关掉即可
小米
AndroidManifest
<receiver
android:name=".push.MiPushReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE"/>
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED"/>
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.ERROR"/>
</intent-filter>
</receiver>
MiPushReceiver
实现PushMessageReceiver,在onNotificationMessageClicked
处理点击事件即可
public class MiPushReceiver extends PushMessageReceiver {
private static final String TAG = "MiPushReceiver";
public static final String APP_ID = "*****";
public static final String APP_KEY = "*****";
@Override
public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
if (DEBUG) {
Logger.d("接收到消息推送透传消息 message: %s", message);
}
PassThroughPushCenter.onReceivePush(message.getContent(), PassThroughPushCenter.TYPE_MI_PUSH);
}
@Override
public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
super.onNotificationMessageArrived(context, miPushMessage);
Log.d(TAG, "onNotificationMessageArrived() called with: context = [" + context + "], miPushMessage = [" + miPushMessage + "]");
}
@Override
public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
super.onNotificationMessageClicked(context, miPushMessage);
NotifyPushCenter.onNotifyPush(context, miPushMessage.getContent(), NotifyPushCenter.NotifyPushChannel.MiPush);
}
@Override
public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
String command = message.getCommand();
List<String> arguments = message.getCommandArguments();
String regId = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
if (DEBUG) {
Logger.d("小米推送回调 regId:%s", regId);
}
PushRegisterProxy.registerMiPush();
}
}
}
}
OPPO
暂缺
VIVO
AndroidManifest
<receiver android:name="com.iksocial.queen.push.VivoPushMessageReceiver">
<intent-filter>
<!--接受push消息-->
<action android:name="com.vivo.pushclient.action.RECEIVE"/>
</intent-filter>
</receiver>
VivoPushMessageReceiver
,同样的,onNotificationMessageClicked点击事件处理即可。
public class VivoPushMessageReceiver extends OpenClientPushMessageReceiver {
@Override
public void onNotificationMessageClicked(Context context, UPSNotificationMessage message) {
NotifyPushCenter.onNotifyPush(context, message.getSkipContent(), NotifyPushCenter.NotifyPushChannel.VPush);
}
@Override
public void onReceiveRegId(Context context, String s) {
Log.i(TAG, "onReceiveRegIdvivopush: " + s);
PushRegisterProxy.registerVivoPush();
}
}
网友评论