原来的项目中集成了阿里云推送,下面把一些要点分享给大家。
简介:
阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效、精确、实时的移动推送的同时,极大地降低了开发成本。让开发者最有效地与用户保持连接,从而提高用户活跃度、提高应用的留存率。
集成:
主这里只贴Maven库快速集成
在Project根目录下build.gradle文件中配置maven库URL:
在对应的module下的build.gradle文件中添加对应依赖
注 : 如果在添加以上 abiFilter 配置之后android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
则在 Project 根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
在AndroidManifest文件中设置appKey,appSecret:
注册调用:
1.先在Application中注册云通道。
package com.haocaiqi.voiceannouncements_spd;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.haocaiqi.voiceannouncements_spd.utils.LogUtil;
public class MainApplication extends Application {
private static final String TAG = "Init";
@Override
public void onCreate() {
super.onCreate();
initCloudChannel(this);
}
/**
* 初始化云推送通道
* @param applicationContext
*/
private void initCloudChannel(Context applicationContext) {
PushServiceFactory.init(applicationContext);
final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.register(applicationContext, new CommonCallback() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "init cloudchannel success");
//获取设备号
pushService.getDeviceId();
LogUtil.logConsole("初始化成功 "+response+ " "+pushService.getDeviceId());
}
@Override
public void onFailed(String errorCode, String errorMessage) {
Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
LogUtil.logConsole("初始化失败"+errorMessage);
}
});
}
}
2.通过广播接收推送通知或消息
package com.haocaiqi.voiceannouncements_spd.receiver;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.android.print.sdk.PrinterInstance;
import com.android.qzs.voiceannouncementlibrary.VoiceUtils;
import com.haocaiqi.voiceannouncements_spd.MainActivity;
import com.haocaiqi.voiceannouncements_spd.R;
import com.haocaiqi.voiceannouncements_spd.utils.LogUtil;
import com.haocaiqi.voiceannouncements_spd.utils.MapUtils;
import java.util.Map;
/**
* Created by qzs on 2017/9/1.
*/
public class MyMessageReceiver extends MessageReceiver {
// 消息接收部分的LOG_TAG
public static final String REC_TAG = "receiver";
private PrinterInstance mPrinter;
@Override
public void onNotification(Context context, String title, String summary, Map extraMap) {
// TODO 处理推送通知
// Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
}
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
}
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) {
Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
}
@Override
protected void onNotificationRemoved(Context context, String messageId) {
Log.e("MyMessageReceiver", "onNotificationRemoved");
}
}
要点:
1.移动推送的初始化必须在Application中,不能放到Activity中执行。移动推送在初始化过程中将启动后台进程channel,必须保证应用进程和channel进程都执行到推送初始化代码。
2.后端可以通过设备号进行推送,设备号在Application中的获取,如下:
pushService.getDeviceId();
也可以绑定一个ID号来推送,如下代码:
PushServiceFactory.getCloudPushService().bindAccount(你要绑定的ID, new CommonCallback() {
@Override
public void onSuccess(String s) {
LogUtil.logConsole("阿里推送绑定成功 "+s+ " ");
}
@Override
public void onFailed(String s, String s1) {
LogUtil.logConsole("阿里推送绑定失败 "+s+ " "+s1);
}
});
3.如果你绑定了ID,当你不想接收推送消息时必须解绑,下面是解绑的方法:
PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
@Override
public void onSuccess(String s) {
// Toast.makeText(MainActivity.this, "赞! 阿里云账号绑定成功 ~",
// Toast.LENGTH_SHORT).show();
}
@Override
public void onFailed(String s, String s1) {
// Toast.makeText(MainActivity.this, "赞! 阿里云账号解绑失败 ~",
// Toast.LENGTH_SHORT).show();
}
});
4.如果用户将应用进程手动杀死,不同的ROM对应用进程的权限管理限制不同,部分ROM环境下应用进程可以被拉起响应推送信息,部分机型受限无法被拉起。
5.如果设备是小米或者华为,建议配置小米和华为辅助通道以及小米托管弹窗,可以提升到达率。
6.后台推送的时候设置离线消息保存:pushRequest.setStoreOffline(true);//离线消息是否保存。若保存, 在推送时候,用户即使不在线,下一次上线则会收到,与expirationTime参数配合使用。
7.推送通知和推送消费在广播接收回调的方法不同。